Convert String to Int Array in C++

  1. Use std::getline and std::stoi Functions to Convert string to int Array in C++
  2. Use std::string::find and std::stoi Functions to Convert string to int Array in C++
  3. Use std::copy and std::remove_if Functions to Convert string to int Array in C++

This article will demonstrate multiple methods about how to convert string to int array in C++.

Use std::getline and std::stoi Functions to Convert string to int Array in C++

std::stoi is used to convert string values to a signed integer, and it takes one mandatory argument of type std::string. Optionally, the function can take 2 additional arguments, the first of which can be used to store the index of the last unconverted character. The third argument can optionally specify the number base of the input. Note that we assume the comma-separated numbers as the input string like .csv file. Thus, we use the getline function to parse each number and then pass the value to the stoi. We also store each number in the std::vector container by calling the push_back method on every iteration.

#include <iostream>
#include <vector>
#include <sstream>
#include <iterator>

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::stringstream;
using std::stoi;

int main(int argc, char *argv[]) {
    string text = "125, 44, 24, 5543, 111";
    vector<int> numbers;
    int num;

    stringstream text_stream(text);
    string item;
    while (std::getline(text_stream, item, ',')) {
        numbers.push_back(stoi(item));
    }

    for (auto &n : numbers) {
        cout << n << endl;
    }

    exit(EXIT_SUCCESS);
}

Output:

125
44
24
5543
111

Use std::string::find and std::stoi Functions to Convert string to int Array in C++

Alternatively, we can utilize a find built-in method of the std::string class to retrieve the position of the comma delimiter and then call the substr function to get the number. Then, the substr result is passed to the stoi which itself is chained to push_back method to store the number into the vector. Note that there’s the line after the while loop which is necessary to extract the last number in the sequence.

#include <iostream>
#include <vector>
#include <sstream>
#include <iterator>

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::stringstream;
using std::stoi;

int main(int argc, char *argv[]) {
    string text = "125, 44, 24, 5543, 111";
    vector<int> numbers;

    size_t pos = 0;
    while ((pos = text.find(',')) != string::npos) {
        numbers.push_back(stoi(text.substr(0, pos)));
        text.erase(0, pos + 1);
    }
    numbers.push_back(stoi(text.substr(0, pos)));

    for (auto &n : numbers) {
        cout << n << endl;
    }

    exit(EXIT_SUCCESS);
}

Output:

125
44
24
5543
111

Use std::copy and std::remove_if Functions to Convert string to int Array in C++

Another method to extract integers is to use std::copy algorithm in conjunction with std::istream_iterator and std::back_inserter. This solution stores the string values into a vector and outputs them to the cout stream, but one can easily add std::stoi function to convert each element to int value. Mind though, that the following example code only stores the numbers as string values.

#include <iostream>
#include <vector>
#include <sstream>
#include <iterator>

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::stringstream;
using std::stoi;

int main(int argc, char *argv[]) {
    string text = "125, 44, 24, 5543, 111";

    vector<string> nums;
    std::istringstream iss(text);
    copy(std::istream_iterator<string>(iss),
         std::istream_iterator<string>(),
                 std::back_inserter(nums));


    for (auto &n : nums) {
        n.erase(std::remove_if(n.begin(), n.end(), ispunct), n.end());
        cout << n << endl;
    }

    exit(EXIT_SUCCESS);
}

Output:

125
44
24
5543
111
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ String

  • Parse String Using a Delimiter in C++