473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

best method to convert list of string to vector<int>?

33
what's the best way to convert a list of string to a vector<int>?
what i have now is for loop and iterate each char in the string
Expand|Select|Wrap|Line Numbers
  1. string base="[0 3 ; 6 9 ; 12 15 ; 18 21]";
  2.  
  3.  
  4. //i have vector<int>row,vector < vector<int>row > mat;
  5. //what i want to do with the string is that everytime char != ";"
  6.  
  7. row.pushback(str that convert to int)
  8.  
  9. //do this when char == ";"
  10.  
  11. mat.pushback(row);
  12.  
but the problem with my code is that when it comes to more than one char for an int (eg "18") im not sure how to make it as (18) without making to many if statement?
and i cant find myself a better way for the code to operate fast.

any help would be great

thanks in advanced
Oct 12 '09 #1
1 4371
Banfa
9,065 Expert Mod 8TB
You have a number of options, place you string into a stringstream then extract the data in the order you expect. One small problem of this is you kind of have to know exactly what you are getting out of the string before you get it, that is it is quite hard to deal with multiple formats for instance a matrix with a variable number of values on each row. If you don't know it's 2 values per row before you start you could have trouble, if you do know that then it is relatively easy to deal with differing numbers of rows based on the character read at the end of the row.

Expand|Select|Wrap|Line Numbers
  1.     string myString = "<123>";
  2.     istringstream ss(myString);
  3.     char sential;
  4.     int value;
  5.  
  6.     ss >> sential;
  7.  
  8.     if (sentinal == '<')
  9.     {
  10.         ss >> value;
  11.  
  12.         cout << value << endl;
  13.     }
  14.  
Alternatively there are various functions in the C standard library for converting strings to numbers strtol being my favourite.
Oct 12 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Erik Borgstr?m | last post by:
Hi, Yes, I have followed some of the discussion on vector<vector<int> >, but perhaps I'm just blind. I want to use a vector<vector<int> > of outer size n and inner size m. Are these correct ways...
1
by: Alex Vinokur | last post by:
------ foo.cpp ------ #include <vector> using namespace std; int main() { const vector<int> v1 (10); const vector<bool> v2 (10); &v1;
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
6
by: Roman Töngi | last post by:
I want to change a vector in a function. I pass a pointer of it to the function and append an item. Then I want to print the first item in the vector. It doesn't work. Can anyone help me? Thanks...
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
0
by: citystud | last post by:
does C# support vector< vector<int> > ?if not how can i use vector?
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.