Posts Tagged ‘char’

Counting the number of occurrences of a given character in a std::string can be done using one function from the STL library: the std::count(…) function. This function takes three parameters: two iterators (the beginning and end of the desired search), and the item you wish to count. This function can be used for any STL […]

Imagine that you have read in a hexadecimal string from the command line, a config file or whatever, and you want to use this value in your program. To do this, you need to do a string to integer conversion, but in base 16 since it’s a hexadecimal string. To do this, you can use […]

Struct sizes in C++

Posted: 24th March 2012 by Tim in C++
Tags: , , , , , , , , , , ,

Consider the following program: #include <iostream> struct A {     char a;     long b;     char c; }; struct B {     char a;     char c;     long b; }; int main() {     A a;     B b;     std::cout << “sizeof(a): ” << sizeof(a) << std::endl;     std::cout << “sizeof(b): ” << sizeof(b) << std::endl;     return 0; } To summarize, it […]