Hi everyone..i'm new c++ programming and i would like to know if its posible to convert a string to a vector array...(Eg string s = "Hello everyone" array a[1]='H', a[2]='e'....)I need a vector array because i do not know what my string will be...it will change everytime...If it is posible i would please like to know the solution...

Recommended Answers

All 4 Replies

why not just index into the string? You don't need a vector just to get a character from the string object.

std::string s = "Hello World";

for(size_t i = 0; i < s.length(); i++)
{
   char c = s[i];
   cout << c << "\n";
}

If you really really want to make it a vector, just do puch_back on the vector instead of cout in the above code.

Thanks a lot mate...this really helped me out :D...however for the sake of knowing, how do i use the push_back??i tried

push_back << c << "\n";

didn't work..:(

piush_back is a method of the vector class --

vector<char> array;

array.push_back('A');

All c++ text books have examples of vector class and how to use them. You should read it.

Your syntax seems to be incorrect.

Visit this link for the correct usage of push_back of vector;

http://www.cppreference.com/cppvector/push_back.html

commented: it's not neccesary to comment on everything. above post eas sufficient +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.