-4

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 :)

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • 1
    Possible duplicate of [How to parse a string to an int in C++?](http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) – LogicStuff Oct 05 '16 at 21:46
  • I may have flagged the wrong dupe because of the misleading title (`int` -> `int`s). Anyways, read the second answer linked. **Do** use `std::istringstream` for this. And dump that `char *`. Why is it not `std::string`? – LogicStuff Oct 05 '16 at 21:53

2 Answers2

1

Your code is pretty close. The problem is that you never push the last number in char_cipher onto cipher_int, because there's no space after it. You need to do an extra check after the loop is done.

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];
    }
}
if (s != "") {
    cipher_int.push(back(stoi(s));
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Let the STL do the parsing for you. You can use a std::istringstream for that, eg:

#include <string>
#include <sstream>
#include <vector>

std::string str_cipher; //"11 2 123 44"
std::vector<int> cipher_int;

std::istringstream iss(str_cipher);
int num;

while (iss >> num) {
    cipher_int.push_back(num);
}

Alternatively:

#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>

std::string str_cipher; //"11 2 123 44"
std::vector<int> cipher_int;

std::istringstream iss(str_cipher);

std::copy(
    std::istream_iterator<int>(iss),
    std::istream_iterator<int>(),
    std::back_inserter(cipher_int)
);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • So currently I am reading the file using the following method. http://www.cplusplus.com/reference/istream/istream/read/ is there a better way to read it in conjection with the method you mention? Thanks – austinx43 Oct 15 '16 at 16:21
  • @austinx43 if the file is being read using an `istream`, like `ifstream`, then you could simply use that `istream` as-is with `operator>>` or `istream_iterator` without using a separate `istringstream`. Hard to tell you what is best without seeing the rest of your code, or the actual file content. – Remy Lebeau Oct 15 '16 at 18:55