Copying part of a vector element to a string variable

S

Steph W.

I know you can copy a vector to a string by using:

std::string t( v.begin(), v.end() );

But is it possible just to copy apart of a vector element to a string?

For instance if you have a vector that has the following:

S R A
F B 30
M A 49
F M 34

Each line would be an element and each char would be an element inside that element. So could take vector[1][4:5], which be the "30", and just put that into a string variable?
 
A

alf.p.steinbach

For instance if you have a vector that has the following:

S R A
F B 30
M A 49
F M 34

Each line would be an element and each char would be an element inside that element. So could take vector[1][4:5], which be the "30", and just put that into a string variable?

Code:
#include <iostream>
#include <utility>      // std::begin, std::end
#include <vector>
#include <string>
using namespace std;

auto main() -> int
{
string const data[] =
{
"S R A",
"F B 30",
"M A 49",
"F M 34"
};

vector<string> const v( begin( data ), end( data ) );
string const s = v[1].substr( 4 );

cout << "'" << s << "'." << endl;
}


Cheers & hth.,

- Alf
 
N

Nobody

I know you can copy a vector to a string by using:

std::string t( v.begin(), v.end() );

But is it possible just to copy apart of a vector element to a string?

The above constructor overload is a template which works with anything
which conforms to the "input iterator" concept. Pointers would work, as
would adding offsets to a random access iterator (such as v.begin()).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top