I am new to C++ and I am creating a small program. Part of it I have created a way of converting a char array to set of int's. But i was just wondering if there is a better way (more efficient and/or uses better C++ standards). Like for example using atoi on spliced part of the array for each number for etc.
So I start of reading a set of numbers e.g. "11 2 123 44" from a file into a char * array and now want to convert them into there respective values currently doing it as follows:
//char * char_cipher; //{'1', '1', ' ', '2', ... , '4'}
//int length; //length of char_cipher
string s = "";
vector<int> cipher_int;
for (int i = 0; i <= length; i++) {
if (char_cipher[i] == ' ') {
//create num
cipher_int.push_back(stoi(s));
s = ""; //empty num
}
else {
//add to string
s += char_cipher[i];
}
}
Any help would be much appreciated thanks :)