Help - Creating numbered vareables c++

using C style strings is so last decade. use STL whenever possible for C++

Code:
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

using namespace std;

template < class T >

string ToString(const T &foo){
	ostringstream	baz;
	baz << foo;
	return(baz.str());
}

//
int main(int argc, char *argv[]){
//do whatever else you need to do

int i;
const int maxloops=10;
vector<string> stringvector;

for(i=0; i < maxloops; ++i){
 stringvector.push_back("this is the " + ToString (i) + " loop"); 
//push a string into the vector and increase the vector's length by 1
}

for (i=0; i< maxloops; ++i){
 cout << stringvector.at(i) << endl;
// use the at function for safe member access
// will throw an exception if i > stringvector.size() 
// the C-style [] operator will not. better to try/catch exception
}

return 0;
}
 
Last edited:
Back
Top