A
A
Artem_r662018-11-22 17:09:53
Python
Artem_r66, 2018-11-22 17:09:53

How to do something similar in C++?

Hello, I'm just learning C ++, the question arose, in Python you can do this:

a = ("   10 20 45 16    70 20   ")
b = a.strip().split()
print(b)
c0 = int(b[2])
c1 = int(b[3])
c2 = c0 + c1
print(c2)
input("\n  Для продолжения нажмите Enter")

How to do the same in C++?
Split strings into numbers and access by index?
I found an example with Boost, but it is still difficult for me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-11-22
@Artem_r66

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

using namespace std;

int main()
{
    string s = "10 20 30   50 99         786 521        3";
    istringstream is(s);
    vector<int> c;

    copy(istream_iterator<int>(is), {}, back_inserter(c));

    for(int i : c)
    {
      cout << i << ' ';
    }

    int j = c[1] + c[2];

    cout << "j == " << j;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question