Sign in to follow this  

From a Vector of string to an array of ints

This topic is 4396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a tile map with comma delimited tile codes in it. Ive read the file, broke it down into its correct numbers i.e 123,44,67 into -> 123 44 67 And stored them in vector i want to take them out of the vector and bung them in an array of ints, but whats the best way to cast them to ints? i tried a stringstream in a for(0 -> vector.size) loop but it seemed to just spit out the first number it converted over and over. What the best way of going about this?

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
.... or make sure that you clear() the flags on the stringstream after every extraction.

2 things....
1)Thanks.
&
2)Whoops, whats a silly thing for me to overlook.

Share this post


Link to post
Share on other sites
Try reading them directly into an vector of ints (and then creating an array from that without any "casting", if you *really* need to make such an array).


vector<int> result;
int current;
char comma;
while(file >> current) {
result.push_back(current);
file >> comma;
// optionally check if it actually was a comma.
}


Better yet, separate the values in your file with whitespace instead of commas so that you don't have to worry about that part of the parsing.

Share this post


Link to post
Share on other sites
Zahlman, so if i whitespace delimit can I read directly into a an int?
How is this done?
At the moment im doing:


while ((getline(ReadMap,ReadIn,',')) != NULL)
{
LevelList.push_back(ReadIn);
}




To read into a string(ReadIn) but if i could just read the delimited numbers into an int it would save time and coding.

Cheers.

Share this post


Link to post
Share on other sites
Let's assume you have a file that has nothing but integers in it. You can read it into a vector like so:

std::ifstream ifs("filename");
std::istream_iterator begin(ifs);
std::istream_iterator end;
std::vector<int> int_vector(begin, end);

This will be equivalent to using operator>> to read in every int. Of course this isn't appropriate if your file has any structure further than just being a list of ints.

Share this post


Link to post
Share on other sites
Quote:
Original post by JDUK
Zahlman, so if i whitespace delimit can I read directly into a an int?
How is this done?


For a single int, with the usual stream extraction operator:

(stream object) >> (int variable);

Share this post


Link to post
Share on other sites

This topic is 4396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this