Archive

Posts Tagged ‘Code Snippets C++’

Load a file to a string list

January 22nd, 2010 No comments
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>

int main(int argc, char* argv[])
{
    std::vector<std::string> v;
    std::ifstream f("e:/test.txt");
    if(f.is_open())
    {
        std::copy(std::istream_iterator<std::string>(f),
                  std::istream_iterator<std::string>(),
                  std::back_inserter(v));
    }
    // проверка
    std::copy(v.begin(), v.end(),
              std::ostream_iterator<std::string>(std::cout, "\n"));

    return 0;
}

rsdn.ru

Convert __DATE__ to unsigned int

January 21st, 2010 No comments
#define YEAR ((((__DATE__ [7] - '0') * 10 + (__DATE__ [8] - '0')) * 10 \
              + (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0'))

#define MONTH (__DATE__ [2] == 'n' ? 0 \
               : __DATE__ [2] == 'b' ? 1 \
               : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 2 : 3) \
               : __DATE__ [2] == 'y' ? 4 \
               : __DATE__ [2] == 'n' ? 5 \
               : __DATE__ [2] == 'l' ? 6 \
               : __DATE__ [2] == 'g' ? 7 \
               : __DATE__ [2] == 'p' ? 8 \
               : __DATE__ [2] == 't' ? 9 \
               : __DATE__ [2] == 'v' ? 10 : 11)

#define DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 + (__DATE__ [5] - '0'))

#define DATE_AS_INT (((YEAR - 2000) * 12 + MONTH) * 31 + DAY)

int main (void)
{
  printf ("%d-%02d-%02d = %d\n", YEAR, MONTH + 1, DAY, DATE_AS_INT);
  return 0;
}

Example of overloading method

May 18th, 2009 No comments
class COverload
{
public:
  COverload(int p) : m_intValue(p)
  { std::cout << "COverload::COverload() " << m_intValue << std::endl; };

  virtual ~COverload()
  { std::cout << "COverload::~COverload()" << std::endl; };

public:
  void overload(void)
  { std::cout << "COverload::overload() " << ++m_intValue << std::endl; };

  void overload(void) const
  { std::cout << "COverload::overload() const " << m_intValue << std::endl; };

protected:
  int m_intValue;
};

int main(void)
{
  COverload const dd(2);
  dd.overload();

  COverload bb(3);
  bb.overload();
}

std::map – копирование элементов

January 14th, 2009 No comments
std::map<wstring, wstring> map1;
std::map<wstring, wstring> map2(map1);
std::map<wstring, wstring> map3(map2.begin(), map2.end());

map1.insert(map3.begin(), map3.end());
map2 = map3;
std::copy(map2.begin(), map2.end(), inserter(map1, map1.end()));