How can I copy some range of element from a vector<string> to a string without an explicit loop?

F

Fred

Hi:
I've got the following request: (suppose the required head file
is included)

vector<string> vec;
// ..........
// push some items into vec

string str; // here I want to
copy some range of elements from vec to str, for example, from vec[2]
to vec[5]

copy( vec.begin()+2, vec.begin()+5,
str.begin() ); // Fail

string temp( vec.begin()+2,
vec.begin()+5 ); // Construct a temporary string containing vec[2] to
vec[5] and then copy temp to str, fail again

So, is there anyway that without starting an explicit loop but
achieve my goal? Thanks for help!
 
G

Gianni Mariani

Fred said:
Hi:
I've got the following request: (suppose the required head file
is included)

vector<string> vec;

You mean :

std::vector<char> vec;

Right ?

std::string( &vec[2], &vec[5]+1 );

Should work.
// ..........
// push some items into vec

string str; // here I want to
copy some range of elements from vec to str, for example, from vec[2]
to vec[5]

copy( vec.begin()+2, vec.begin()+5,
str.begin() ); // Fail

string temp( vec.begin()+2,
vec.begin()+5 ); // Construct a temporary string containing vec[2] to
vec[5] and then copy temp to str, fail again

So, is there anyway that without starting an explicit loop but
achieve my goal? Thanks for help!
 
F

Fred

You mean :

std::vector<char> vec;

Right ?

In fact not said:
std::string( &vec[2], &vec[5]+1 );

Should work.

Agree.

Thanks for answering, but my problem remains unresolved.
// ..........
// push some items into vec
string str; // here I want to
copy some range of elements from vec to str, for example, from vec[2]
to vec[5]
copy( vec.begin()+2, vec.begin()+5,
str.begin() ); // Fail
string temp( vec.begin()+2,
vec.begin()+5 ); // Construct a temporary string containing vec[2] to
vec[5] and then copy temp to str, fail again
So, is there anyway that without starting an explicit loop but
achieve my goal? Thanks for help!- Hide quoted text -

- Show quoted text -
 
K

Kishore Yada

Fred said:
Hi:
I've got the following request: (suppose the required head file
is included)

vector<string> vec;
// ..........
// push some items into vec

string str; // here I want to
copy some range of elements from vec to str, for example, from vec[2]
to vec[5]

copy( vec.begin()+2, vec.begin()+5,
str.begin() ); // Fail

string temp( vec.begin()+2,
vec.begin()+5 ); // Construct a temporary string containing vec[2] to
vec[5] and then copy temp to str, fail again

So, is there anyway that without starting an explicit loop but
achieve my goal? Thanks for help!

I think accumulate should work.

str = accumulate( vec.begin()+2, vec.begin()+5, string() );

I could not test if the above code compiles as I do not have a C++
compiler with me at present.
 
A

Alan Johnson

Fred said:
You mean :

std::vector<char> vec;

Right ?

In fact not said:
std::string( &vec[2], &vec[5]+1 );

Should work.

Agree.

Thanks for answering, but my problem remains unresolved.
// ..........
// push some items into vec
string str; // here I want to
copy some range of elements from vec to str, for example, from vec[2]
to vec[5]
copy( vec.begin()+2, vec.begin()+5,
str.begin() ); // Fail
string temp( vec.begin()+2,
vec.begin()+5 ); // Construct a temporary string containing vec[2] to
vec[5] and then copy temp to str, fail again
So, is there anyway that without starting an explicit loop but
achieve my goal? Thanks for help!- Hide quoted text -
- Show quoted text -

It's hard to tell exactly what behavior you want. Is this it, maybe?

#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <iostream>

using namespace std ;

int main()
{
vector<string> strings ;
strings.push_back("String0") ;
strings.push_back("String1") ;
strings.push_back("String2") ;
strings.push_back("String3") ;
strings.push_back("String4") ;
strings.push_back("String5") ;
strings.push_back("String6") ;

stringstream ss ;
copy(strings.begin() + 2, strings.begin() + 6,
ostream_iterator<string>(ss, " ")) ;

cout << ss.str() << endl ;
}
 
P

peter koch

Hi:
I've got the following request: (suppose the required head file
is included)

vector<string> vec;
// ..........
// push some items into vec

string str; // here I want to
copy some range of elements from vec to str, for example, from vec[2]
to vec[5]

copy( vec.begin()+2, vec.begin()+5,
str.begin() ); // Fail

string temp( vec.begin()+2,
vec.begin()+5 ); // Construct a temporary string containing vec[2] to
vec[5] and then copy temp to str, fail again

So, is there anyway that without starting an explicit loop but
achieve my goal? Thanks for help!

My approach would be to use a stringstream. Something like:

std::vector<std::string> vec;
std::stringstream ss;
std::eek:stream_iterator<std::string> s(ss," ");
std::string str;
...
std::copy(vec.begin() + 2,vec.begin() +
5,std::eek:stream_iterator<std::string>(ss," "));
std::string str = ss.str();

The advantage is that this allows a separator between the elements (if
so desired) and that you can use any type of vector so long as its
elements are streamable. You could create a (templated) function to
simplify use.
Kishore Yadas idea is also nice. It is simpler but restricted to
containers of strings.

/Peter
 

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
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top