I have a std::string
that contains a random phrase.
For example:
std::string str{ "Lorem ipsum" };
I'd like to take that string and convert it into a std::vector<std::vector<char>>
(each sub-array would have all the characters for one word in the phrase, i.e. splitting on spaces).
For example after the conversion:
std::vector<std::vector<char>> arr{ { 'L', 'o', 'r', 'e', 'm' }, { 'i', 'p', 's', 'u', 'm' } };
Furthermore, I'd like to then be able to convert the std::vector<std::vector<char>>
back into a std::string
.
Thank you for the help!
CodePudding user response:
#include <bits/stdc .h>
using namespace std;
int main()
{
vector<vector<char>> v;
vector<char> c;
string s = "hello world this";
for(int i=0;i<s.size();i )
{
if(s[i]==' ')
{
v.push_back(c);
c.clear();
}else{
c.push_back(s[i]);
}
}
for(int i =0;i<v.size();i )
{
for(int j=0;j<v[i].size();j )
{
cout<<v[i][j];
}
cout<<endl;
}
return 0;
}