473,883 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<stringve c;
// ..........
// 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 2402
Fred wrote:
Hi:
I've got the following request: (suppose the required head file
is included)

vector<stringve c;
You mean :

std::vector<cha r 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...@mari ani.wswrote:
Fred wrote:
Hi:
I've got the following request: (suppose the required head file
is included)
vector<stringve c;

You mean :

std::vector<cha r 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<stringve c;
// ..........
// 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...@mari ani.wswrote:
>Fred wrote:
>>Hi:
I've got the following request: (suppose the required head file
is included)
vector<stringve c;
You mean :

std::vector<cha r 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<stringst rings ;
strings.push_ba ck("String0") ;
strings.push_ba ck("String1") ;
strings.push_ba ck("String2") ;
strings.push_ba ck("String3") ;
strings.push_ba ck("String4") ;
strings.push_ba ck("String5") ;
strings.push_ba ck("String6") ;

stringstream ss ;
copy(strings.be gin() + 2, strings.begin() + 6,
ostream_iterato r<string>(ss, " ")) ;

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

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

vector<stringve c;
// ..........
// 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::stringstre am ss;
std::ostream_it erator<std::str ings(ss," ");
std::string str;
...
std::copy(vec.b egin() + 2,vec.begin() +
5,std::ostream_ iterator<std::s tring>(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
18044
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 application using C++ and the STL for handling my data. Unfortunately, I must interact with a (vanilla) C API. I use vectors of strings (for simplicity and less memory hassle), but the function calls for this API require arrays of character...
10
3113
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
2063
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, sizeof(levelfiles) ); levelfiles = fromCharS(inc); fin.close();
2
2643
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 ? // test.cpp #include <iostream> #include <fstream> #include <vector> #include <string>
5
3557
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
2504
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! --Shafik
6
5730
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
5
15240
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
7393
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", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
42
4576
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 of string, in one line. I could
0
9789
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11129
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10838
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10408
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7967
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7119
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.