get a string and put in a vector<int>

I want to do a program to sort integers in a vector...that part is done..my trouble is that I want to get that integers from a string and put them in a vector....I have done this, thinking that as a string is null terminated as a char array I could send a char pointer to a function to get an string....but not, i have done this at the moment....

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
30
31
32
33
34
  
#include <vector>
 #include <iostream>
 #include <algorithm>
 #include <cstring>
 
 using namespace std;
 
 
 void put_order(char* str){
	 vector<int> holder;
	 for(unsigned int i = 0;i != strlen(str);i++){
		 holder.push_back(str[i]);
	 }
	 
	 sort(holder.begin(),holder.end());
	 
	 for(vector<int>::iterator it = holder.begin(); it != holder.end();it++){
		 cout<<*it<<" ";
	 }
	 
	 
 }
 
 int main(){
	 string str;
	 cout<<"Enter any random numbers"<<endl;
	 cin>>str;
	 put_order(str);
	 return 0;
 }


Change line 10 to void put_order(std::string const &str){

Why do you even need the vector? You can iterate over strings the same way.
Last edited on
yes, I wanted to do it on a vector, what I wanted to do was transfer int from a string to a vector....I did 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
30
31
32
33
#include <iostream>
 #include <algorithm>
 #include <cstring>    
 #include <sstream>
 
 
 using namespace std;
 
 
 void put_order(string& str){
	 vector<int> holder;
	 stringstream line(str);
	int num;
	while(line>>num){
		holder.push_back(num);
	}
	 
	 sort(holder.begin(),holder.end());
	 
	 for(vector<int>::iterator it = holder.begin(); it != holder.end();it++){
		 cout<<*it<<" ";
	 }
	 
	 
 }
 
 int main(){
	 string str;
	 cout<<"Enter any random numbers"<<endl;
	 getline(cin,str);
	 put_order(str);
	 return 0;
 }



but I would like to know they way you suggested too...
Ah, converting a string to a number is significantly different than your original code. My suggestion is no longer relevant ;)
hey, I have seen your website and you look like somebody who could give good tips...

I'm electronic engineer but in fact I don't like the Electronic ( perfect situation eh), lately I have been studying C++ by myself because I like more this world and amazingly I have done a couple on interview for job as a C++ developer...I wasn't successful but the gave to me test and exercises to do and I didn't manage too bad...the point is I think I should collaborate in some projects, but getting a place to collaborate is being difficult, how could I get into a project to collaborate and start to have a wider vision of this stuff....

I hope you can give any idea or even if you are involved in something and you think I could give a hand I'm willing.....
I first recommend getting comfortable with modern C++ - maybe buy a book to read, for instance:
http://stackoverflow.com/a/388282/1959975

A good way to start is by making simple games, e.g. try to replicate Pong and then add more features to it. You can use a graphics library like SFML or Magnum - don't bother with the console. After Pong you could try recreating Mario - take advantage of classes and polymorphism for the enemies.

Once you have more experience:
Create a GitHub account and look for small open source C++ projects. Read the issues they have and see if you can fix them, then submit your pull request and see what the author says. This is the true test: if the world at large accepts your code, you must be doing something right.
I'm reading this book right now The C++ Programming Language by Bjarne Stroustrup and well I do small projects...I have done a few projects too, methods to simplificate booleans functions.....such as quine McCluskey and BCD logic......what you have said about Pong...i don't know what you mean create a real game with graphics libraries?? and how I could compare my one with the real one, is there repository where they are a lot code stored....???
Check out SFML:
http://www.sfml-dev.org/
I'm having a look of that, and that's great...

I have been coding with codelite and codeblock, I have never done a a real application everything was always on console...I don't know if I should have knowledge of those thing to become a developer...I remeber that I did so small things when I was a kid with visual basic.....

I don't know how I should do this....I mean I shoudl download visula basic c++ and code on that or I could link my codes from codelite to the SFML platform??.....I must surely be saying idiots things but I'm a completely ignorant at that...but I don't know ...to do a big project there must be people working on raw code and other doing other stuff....
You can use SFML with Visual Studio 2015, just make sure to install the C++ components of Visual Studio 2015 when you navigate through the installer.

Visual Studio 2015 community is free and has good modern C++ support:
https://www.visualstudio.com/
I though SFML was like a set of libraries with some feature that I could use like a headers files in my typical codelite...
Yes, I misunderstood your post, sorry. You can use SFML with any compiler you want on any system you want.
Topic archived. No new replies allowed.