Unpack a pair


Hi.

thats pretty much it.

I have a string with 2 numbers. I want to asign them to different variables
There's several ways to do this. One is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <sstream>
#include <iostream>
#include <string>

int main()
{
    std::string test {"23 45"};
    std::istringstream iss(test);

    int a, b;

    iss >> a >> b;

    std::cout << a << " " << b << '\n';
}

Hello Moris526,

Before you get several answers that are beyond what you know it would help if you mention what you have learned and what you can and can not use.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string str = "3.14159 2.7828";
   size_t pos;
   double a = stod( str, &pos );
   double b = stod( str.substr( pos ) );
   cout << a << '\n';
   cout << b << '\n';
}


That worked.

Thank you for all the answers
Topic archived. No new replies allowed.