I don't know a way to enter string into a vector.
For example, there is string s = "augcuuaucaca" and I want to separate the string s into groups of 3 characters such as "aug", "cuu", "auc", "aca" and each group of characters would be string type.
After that, I would store "aug" into vector[0], "cuu" into vector[1], etc...
So each element of vector would have a 3 character group.

I tried many different ways but I can't seem to find the solution. I need help how to solve this.
Below are some of my attemps to solve this the problem.

``````cout << s << endl;
for (int i=0; i < s.length() - 2; i+3)
{
char temp[3];
cin.get (temp, 3);
codons.push_back (temp);
cout << "The codons: " << codons[i] << endl;
}``````
``````vector<char> codons;

for (int i=0; i < dnaStrand.length() - 2; i+3)
{
char temp[] = { dnaStrand[i], dnaStrand[i+1], dnaStrand[i+2] };
codons.push_back (temp);
}``````
``````vector<string> codons;

for (int i=0; i < dnaStrand.length() - 2; i+3)
{
char temp[] = { dnaStrand[i], dnaStrand[i+1], dnaStrand[i+2] };
codons.push_back (temp);
}``````
2
Contributors
1
3
Views
6 Years
Discussion Span
Last Post by Clinton Portis
``````string s("augcuuaucaca");
string temp;
int size = s.size();
int pos = 0;
vector<string> list;

do{
try
{
temp = s.substr(pos, 3);
list.push_back(temp);
pos += 3;
}
catch(out_of_range)
{
break;
}
}while(pos != size);``````

Edited by Clinton Portis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.