STL in C++

STL stands for Standard Template Library in C++. It is very convenient, especially for people who use diverse languages. For example, I would assume there are functions like this:

a = “007”

as.numeric(a)==7 => should be true

In many latest languages, it is very friendly(saving finger muscle movements) that you can find such similar function doing the conversion. But back to C++, it is not the case at all. All the sudden we are back to stone age. No need to panic though. In STL, there are some functions for this purpose–converting string to number. Although still not very convenient, it is better than nothing unless you write your own (perfectly fine).  Suppose you want to pass input values for different variable at command line, there are up to 3 variables, “start”, “end”, “seed”, the STL string is quite useful for this purpose because it has strcmp and atoi to do string comparison and converting string to integers, respectively. Here is how I would do:

#include <string>

using namespace std;

int main(int argc, char *argv[])
{
int StartI, EndI, Seed;

if (argc ==  1){
cout << “Error: no command line inputs!” << endl;
exit(0);
}

for (int ai=1; ai < argc; ai++){
if (strcmp(argv[ai], “-start”)==0)
StartI = atoi(argv[++ai]);
else if (strcmp(argv[ai], “-end”)==0)
EndI = atoi(argv[++ai]);
else if (strcmp(argv[ai], “-seed”)==0)
Seed = atoi(argv[++ai]);
else
cout << “Unknown command line inputs!” << endl;
}
}

There are surely other excellent STLs, for example, vector, list,… But I personally haven’t used them yet since if possible I use other languages (PERL and R) to do such things instead.

Comments are closed.