Archive for the ‘C/C++’ Category

C/C++ — Insert sequential values into container

November 7, 2008

To insert sequential values into a container, you could use the iota function available in ext/numeric header. For example:

#include <ext/numeric>
#include <vector>

std::vector<int> v(5);
__gnu_cxx::iota(v.begin(), v.end(), 0);

v will contain {0, 1, 2, 3, 4}.

For more information see sgi documentation


C/C++ — Friends and Templates

November 3, 2007

In C++, if you declare a template class and you would like to have a friend function that has an argument of the current class type, you need to be careful when specifying the name of the template types. For example:

template <class T>
class A {

template <class T>
friend void b(A<T> a);
} ;

Will confuse the compiler, as you use the same template name, T, for the class template and the function template type. What you need to do, it to use a different name for the function template type:

template <class T>
class A {

template <class V>
friend void b(A<V> a);
} ;

Notice that I used T for the class template type and V for the function template type.

C++ — Convert string to upper/lower case

August 4, 2007

To convert an std::string to upper case you can use the following:

#include <algorithm>
#include <string>

std::string data = “Abc”;
std::transform(data.begin(), data.end(),
data.begin(), ::toupper);

For converting to lower case, you need to replace ::toupper with ::tolower.

Other C/C++ not so frequently asked questions.

C/C++ – _M_underflow_common

February 13, 2007

When you define you own character traits (e.g., ignorecase_traits – to ignore the case when comparing strings) you might run into the following link error:

In function `std::basic_filebuf<char, ignorecase_traits>::uflow()'::
undefined reference to `std::basic_filebuf<char, ignorecase_traits>::

The solution is to add the following to one of your CC/CPP files:

struct cGet_M_underflow_common : std::basic_filebuf<char> {
  int_type mGet_M_underflow_common(bool b) {
    return _M_underflow_common(b);

icfielbuf::int_type icfielbuf::_M_underflow_common(bool bump) {
  return reinterpret_cast<cGet_M_underflow_common *>(this)->

C/C++ – binary IO

February 1, 2007

Here is an example of how we can write to and read from files in binary:

ofstream file(“foo.txt”, ios::out | ios::binary);
unsigned e =101;
file.write(reinterpret_cast<char*>(&e), sizeof(unsigned));

ifstream file(“foo.txt”, ios::in | ios::binary);
unsigned e;<char*>(&e), sizeof(unsigned));

More info:

C/C++ – GCC create a static library

September 1, 2006

You can create a static library from object files using the following command:

ar rcs libfunc.a func1.o func2.o func3.o

To link the libary to your code you can use use:

g++ -o exe_file exe_file.o -L. -lfunc

C++ — Convert int to string

August 30, 2006

In order to convert an int (or any other numeric type, e.g., float, double, etc.) to string, you can use:

#include <sstream>

int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();

Other C/C++ not so frequently asked questions.

C/C++ — printf — Number of chars to output

August 25, 2006

If you want to output a specified number of chars from a char array using a printf-like function, you can use:

char buffer[] = “Hi there!”;
int n = 2;
printf(“%.*s”, n, buffer); // outputs “Hi”

It will output only the first n number of characters form buffer.

C/C++ – non blocking file descriptor

August 24, 2006

If you got an input file descriptor by using open() (<unistd.h>) or other function that creates a file descriptor and you want to set the descriptor to be non blocking on read, you can do:

fcntl(file_desc, F_SETFL, O_NONBLOCK); // <fcntl.h>

C++ – convert object to char*

August 23, 2006

To easily convert an object to char* you can write an operator like:

class String {
char str[80];

operator char *() { return str; } // convert to char*

And the you can use it like this:

char *buf;
String s;
buf =s;

More here: