Posts Tagged c++

using stl replace to replace characters in c-string

C++:

char cstring[10]="example 1";
// search for space and replace with "_"
stl::replace(&cstring[0], &cstring[strlen(cstring)], " ", "_");

, , ,

Leave a comment

singleton in C++

C++:

// not thread safe though
class Singleton {
    private:
        Singleton();
        ~Singleton();
        Singleton(const Singleton&);
        Singleton & operator=(const Singleton&);
    public:
        static Singleton &getInstance() {
            static Singleton instance;
            return instance;
        }
};

Above singleton is achieved by having private constructors. User can use Singleton::getInstance() to get access to the single instance.

, ,

Leave a comment

non-copyable class in C++

C++:

// make class non-copyable by having private copy-constructor and assignment
class NonCopyable {
    private:
        NonCopyable(const NonCopyable &);
        NonCopyable &operator =(const NonCopyable &);
};
// private inheritance from NonCopyable will save you a lot typing
class Object : private NonCopyable {
};

,

Leave a comment

C++ print string in vector separated by spaces

C++:

#include <iostream>
#include <iterator>
// vector of words
std::vector<std::string> words;
// print words and insert spaces to separate words
std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, " "));
// print words without space in-between
std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout));

Very often we want to print sequence of numbers, strings or other types. copy allows you to do it very easily and you don’t have to worry about the delimiter.

 

, , ,

2 Comments

C++ string tokenization / split string

C++:

// standard C++
const string line = "research book example";
istringstream sString(line);
istream_iterator<string> first(sString), last;
vector<string> tokens(first, last);

// use boost library
vector<string> tokens;
boost::split(string, tokens, is_any_of(" "));

The boost library provides many string algorithms, split is one of them.
The stl way can be tricky. The first time I didn’t use local variables first and last, it didn’t work. The trick is that the compiler thinks it is a function (namely tokens) declaration, using local variables helps the compiler to understand the code.

 

, ,

Leave a comment

display source in gdb

gdb:

# display source code window in gdb
Ctrl+x a
# display source code in gdb default mode
list

gdb is the most popular debugger on Linux. I use it all the time and really can’t live without it.

, , ,

Leave a comment

C++ use “\n” instead of endl

C++:

// prefer "\n" to std::endl to a stream
std::cout << "A message ends with newline" << "\n";


Apparently, std::endl does a little bit more then traditional newline “\n“: it will cause the stream to flush its buffer. Because of this, it can be a little bit more expensive than you think. Personally I don’t consider it as a big problem. It is still better to know it is possible to be a performance issue. 

, ,

Leave a comment

C++ prefers ++i to i++

C++:

// loop through a STL container using iterators, pseudo code
for(const_iterator i=container.begin(); i!=container.end(); ++i) {
op(*i);
}
// of course, this loop should be re-written as:
for_each(container.begin(),container.end(), op);

C++ always prefers prefix ++/ to postfix ++/. Why? Prefix ++/– are more efficient than their postfix counterparts: postfix will generate a temporal copy of the variable and increase or decrease the  variable. It is not very obvious in C, however, in C++, it may imply there is a copy constructor being called for every iteration in the loop. It is a good habit to always use prefix in loops.

, ,

Leave a comment