Friday, May 13, 2011

Some C++ Tips and Functions



A simple C++ function to tokenize a string



#include <vector>
#include <string>
#include <sstream>
using namespace std;
vector<string>  getTokens(string str){
 string buf;  // Have a buffer string
 stringstream ss(str);  // Insert the string into a stream
 vector tokens;  // Create vector to hold our words
 while (ss >> buf)
    tokens.push_back(buf);
 return tokens;
}





A more complex tokenize function


void Tokenize(const string& str,
                      vector& tokens,
                      const string& delimiters = " ")
{
     // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
     // Find first "non-delimiter".
    string::size_type pos     = str.find_first_of(delimiters, lastPos);
    while (string::npos != pos || string::npos != lastPos)
    {
         // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
         // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
         // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}
Usage:

 vector<string> tokens;
  string str("Split,me up!Word2 Word3.");
  Tokenize(str, tokens, ",<'> " );
  vector <string>::iterator iter;
  for(iter = tokens.begin();iter!=tokens.end();iter++){
    cout<< (*iter) << endl;
 }

Write a number to a string

int                   myInteger;
string              myString;
ostringstream myStream;
 // Copy Integer to String Stream
myStream << myInteger;
 // Assign characters in stream to std::string
myString = myStream.str();
A stream can be cleared in the following way:
myStream.str("");

Replace a vector element

vector  theVector;
 //populate the vector
theVector.at(2) = "new string";

Transform HTML color code string to COLORREF

The string parameter is assumed to be in '#FFFFFF' format.
COLORREF transformHTML(string color){
 string s1(color, 1,2);
 string s2(color, 3,2);
 string s3(color, 5,2);

 long r = strtol(s1.c_str(), NULL, 16);
 long g = strtol(s2.c_str(), NULL, 16);
 long b = strtol(s3.c_str(), NULL, 16);

 return RGB (r,g,b);
}

Determine Whether A String is in Upper Case

bool isUpper(string line){
 const char *str = line.c_str();
 while(*str != '\0'){
  if(IsCharLower(*str))
   return false;
  *str++;
 }
 return true;
}
A Windows IsCharLower function is used.

Convert str::string to char

Use the c_str() function.

Example:

string str = "test string";
const char *ch = str.c_str();

Pass string arrays to functions

This actually refers to C.
You declare a string array in the calling function as follows:
 char arr[10][30];
The second dimension must always have the exact number of elements in the function declaration:
 int functionName(char  (*arr) [30], int numberOfElements 10);
The brackets around the array name are important. You also pass the number of elements in the first dimension as an argument to the function.