Unpack a pair
Jan 5, 2021 at 12:15pm UTC
Hi.
thats pretty much it.
I have a string with 2 numbers. I want to asign them to different variables
Jan 5, 2021 at 12:34pm UTC
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' ;
}
Jan 5, 2021 at 12:54pm UTC
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
Jan 5, 2021 at 1:29pm UTC
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' ;
}
Jan 6, 2021 at 6:52am UTC
That worked.
Thank you for all the answers
Topic archived. No new replies allowed.