954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to put multiple string into vector?

Hi,
The program I'm trying to make is to transcribe DNA to RNA and separate into codons.
So far I was able to transcribe DNA to RNA and store it in string variable.
Next part is to separate RNA strand into groups of 3 (codon) and store each group in vector.
Below is part of my code.

void GeneLoader::setCodon ()
{
	string s = rna.getRnaStrand ();
	
	for (int i=0; i < s.length() - 2; i+3)
	{
	temp = s[i] + s[i+1] + s[i+2];
		s.push_back (temp);	
	}
}


The string s has the RNA sequence stored inside. I'm trying to concatenate s[i] + s[i+1] + s[i+2] into a one string and store into the vector.
For example, if s[i] == 'a', s[i+1] == 'c', s[i+2] == 'g' then s[i] + s[i+1] + s[i+2] would equal to "acg". The "acg" would be stored into the vector[i] and so forth.
Any advice on how to tackle this problem?

sangkun
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

s[i] + s[i+1] + s[i+2]; would just do an integral addition of the three char values; instead you need to make astd::string of three chars.

I suppose for (int i=0; i < s.length() - 2; i+3) is a typo;
should be for( int i=0 ; i < s.length() - 2 ; i += 3 ) .

Something like:

std::vector< std::string > strand_to_codons( const std::string& strand )
{
    std::vector< std::string > codons ;

    for( std::string::size_type i = 0 ; i < strand.size() - 2 ; i += 3 )
    {
        const char temp[] = { strand[i], strand[i+1], strand[i+2], 0 } ;
        codons.emplace_back(temp) ;
    }

    return codons ;
}


Another option would be to use thesubstr() member function of std::string:

std::vector< std::string > strand_to_codons( const std::string& strand )
{
    std::vector< std::string > codons ;

    for( std::string::size_type i = 0 ; i < strand.size() - 2 ; i += 3 )
            codons.push_back( strand.substr( i, 3 ) ) ;

    return codons ;
}


Yet another way is to construct the substring in situ into thestd::vector<> via an emplace_back() (requires compiler support for variadic templates).

std::vector< std::string > strand_to_codons( const std::string& strand )
{

    std::vector< std::string > codons ;

    for( auto iter = strand.begin() ; iter < strand.end()-2 ; iter += 3 )
            codons.emplace_back( iter, iter+3 ) ;

    return codons ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

I'm a beginner in c++.
I have tried your code but it didn't work.
Also I don't understand the need for "std::string::size_type i = 0".
Please explain.

sangkun
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

> I don't understand the need for "std::string::size_type i = 0".
> Please explain.

std::string::size_type is an alias (a typedef) for some unsigned integral type that is sufficiently large to represent the number of chars in a std::string, no matter how long the string is. An int need not be able to do that on every implementation.

std::string::size_type eliminates the guesswork involved in choosing a suitable integral type to represent the position of a char in a std::string; using it allows us to write code that is portable across implementations.

> I have tried your code but it didn't work.

That came as a surprise to me; so I've just tried compiling and running the code. All three versions of strand_to_codons() work as expected. (With a conforming compiler, of course.)

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: