How to convert string to int array in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <sstream>

int main() {
    std::string s = "3 12 87 901 3268 85828";
    std::vector<int> v;

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

    for (auto &n : v) {
        std::cout << n << "\n";
    }

    return 0;
}




/*
run:

3
12
87
901
3268
85828

*/

 



answered May 8, 2021 by avibootz

Related questions

2 answers 162 views
2 answers 179 views
3 answers 309 views
2 answers 203 views
2 answers 117 views
1 answer 172 views
3 answers 326 views
326 views asked May 16, 2021 by avibootz
...