469,928 Members | 1,873 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,928 developers. It's quick & easy.

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

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

vector<stringvec;
// ..........
// 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!

Feb 7 '07 #1
5 2148
Fred wrote:
Hi:
I've got the following request: (suppose the required head file
is included)

vector<stringvec;
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!
Feb 7 '07 #2
On Feb 7, 4:33 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
Fred wrote:
Hi:
I've got the following request: (suppose the required head file
is included)
vector<stringvec;

You mean :

std::vector<char vec;

Right ?
In fact not, because vec are not necessarily a vector<char>.
>
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 -

Feb 7 '07 #3

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

vector<stringvec;
// ..........
// 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.

Feb 7 '07 #4
Fred wrote:
On Feb 7, 4:33 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>Fred wrote:
>>Hi:
I've got the following request: (suppose the required head file
is included)
vector<stringvec;
You mean :

std::vector<char vec;

Right ?

In fact not, because vec are not necessarily a vector<char>.
>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<stringstrings ;
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 ;
}

--
Alan Johnson
Feb 7 '07 #5
On Feb 7, 9:24 am, "Fred" <hn.ft.p...@gmail.comwrote:
Hi:
I've got the following request: (suppose the required head file
is included)

vector<stringvec;
// ..........
// 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::stringvec;
std::stringstream ss;
std::ostream_iterator<std::strings(ss," ");
std::string str;
...
std::copy(vec.begin() + 2,vec.begin() +
5,std::ostream_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

Feb 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matt Garman | last post: by
10 posts views Thread by dalbosco | last post: by
4 posts views Thread by misirion | last post: by
5 posts views Thread by Gary Wessle | last post: by
10 posts views Thread by Shafik | last post: by
6 posts views Thread by arnuld | last post: by
5 posts views Thread by Peithon | last post: by
42 posts views Thread by barcaroller | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.