472,328 Members | 1,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 2282
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without...
10
by: dalbosco | last post by:
Hello, I am new to STL and I've written the following code which crash. Could anyone tell me why this code crash? Thanks by advance. -- J-F ...
4
by: misirion | last post by:
Ciao, vorrei poter scrivere e leggere su files binari dei vettori di stringhe, ed avrei implementato questo codice: ifstream...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form...
5
by: Gary Wessle | last post by:
whats an efficient way to copy a string to a vector<string>? how about this? #include <iostream> #include <string> #include <vector> Using...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: ...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program...
5
by: Peithon | last post by:
Hi, I'm trying to create a vector of strings and print the contents using an iterator but I'm getting an error with my very first string. Can...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() <<...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.