473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 2362
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 any sacrifice in performance. I'm writing an...
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 #include <iostream>
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 fin(conf.c_str(),ios::binary); char inc; fin.read( inc,...
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 of list< vector<string> > is correct in STL ? //...
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 namespace std;
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: vector<stringv = vector<string>(4); Thanks!...
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 to store each line from a file into a *...
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 anyone help?
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 MapVector; MapVector.insert(make_pair("string1",...
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() << "\n"; Basically, he is trying to print a vector...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.