Converting string into double array

I am having a lot of trouble extracting values from a string into a double array.

I have a string that looks something like this:
"asdf 1.320 1 234.43 5.00000 3"

All I want to do is extract the doubles and store them in an array of doubles.

Any tips?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>

int main() 
{ 
    const std::string input = "asdf 1.320 1 234.43 5.00000 3" ;
    
    // create an input string stream to read from the string
    std::istringstream stm(input) ;
    
    // perform formatted input from the string stream
    std::string str ;
    double d1, d2, d3 ;
    int i1, i2 ;
    if( stm >> str >> d1 >> i1 >> d2 >> d3 >> i2 )
    {
        std::cout << std::fixed << str << '\n' << d1 << '\n' << i1 << '\n' 
                  << d2 << '\n' << d3 << '\n' << i2 << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/a94e94ce88a4fafe
Thanks, that does work for the specific string I provided.


I was wondering how to write a code that satisfies ANY case for any string with a random amount of numbers in it?
How about this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <vector>


int main ( )
{
    std::string str = "asdf 1.320 1 234.43 5.00000 3", temp;

	std::vector<double> vec;


	size_t i = 0, start = 0, end;

	do {
		end = str.find_first_of ( ' ', start );
		temp = str.substr( start, end );
		if ( isdigit ( temp[0] ) )
		{
			vec.push_back ( atof ( temp.c_str ( ) ) );
			++i;
		}
		start = end + 1;
	} while ( start );


	for ( i = 0; i < vec.size ( ); ++i )
		std::cout << vec[i] << '\n';
}


Edit: I decided to use a vector instead of an array.
Last edited on
That was very helpful, thankyou
> for any string with a random amount of numbers in it?

Read the numbers into a dynamically resizeable array (a vector)
http://www.mochima.com/tutorials/vectors.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>

int main() 
{ 
    const std::string input = "asdf 1.320 1 234.43 5.00000 3 23.4 86.7 -32" ;
    
    // create an input string stream to read from the string
    std::istringstream stm(input) ;
    
    // throw away the first string
    std::string str ;
    stm >> str ;
    
    // read the  numbers into a vector
    std::vector<double> numbers ;
    double n ;
    while( stm >> n ) numbers.push_back(n) ;
    
    for( double d : numbers ) std::cout << std::fixed << d << ' ' ;
}

http://coliru.stacked-crooked.com/a/fb3bc358303a5e5c
If you only want the doubles and not the ints, use this do..while instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
do {
	end = str.find_first_of ( ' ', start );
	temp = str.substr( start, end );
	if ( isdigit ( temp[0] ) )
	{
		number = atof ( temp.c_str ( ) );
		if ( number != int ( number ) )
		{
			vec.push_back ( atof ( temp.c_str ( ) ) );
			++i;
		}
	}
	start = end + 1;
} while ( start );
Wow that was exactly what I needed, thanks.

Out of curiosity, how the heck do you guys just write these codes in like 3 minutes?
Lots of practice. :D
Topic archived. No new replies allowed.