473,322 Members | 1,408 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,322 software developers and data experts.

copy string[] to vector<string>


whats an efficient way to copy a string[] to a vector<string>?

how about this?

#include <iostream>
#include <string>
#include <vector>

Using namespace std;

int main(){
string s[] = { "aaa", "bbb", "ccc" };
vector<stringv( s.begin(), s.end() );
cout << v[1] << endl;
}

Oct 30 '06 #1
5 3508
* Gary Wessle:
whats an efficient way to copy a string[] to a vector<string>?

how about this?

#include <iostream>
#include <string>
#include <vector>

Using namespace std;

int main(){
string s[] = { "aaa", "bbb", "ccc" };
vector<stringv( s.begin(), s.end() );
cout << v[1] << endl;
}
That won't compile.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 30 '06 #2
"Alf P. Steinbach" <al***@start.nowrites:
* Gary Wessle:
whats an efficient way to copy a string[] to a vector<string>?
how about this?
#include <iostream>
#include <string>
#include <vector>
Using namespace std;
int main(){
string s[] = { "aaa", "bbb", "ccc" };
vector<stringv( s.begin(), s.end() );
cout << v[1] << endl;
}

That won't compile.
yes, it did not compile.
Oct 30 '06 #3

Gary Wessle wrote:
whats an efficient way to copy a string[] to a vector<string>?

how about this?

#include <iostream>
#include <string>
#include <vector>

Using namespace std;

int main(){
string s[] = { "aaa", "bbb", "ccc" };
vector<stringv( s.begin(), s.end() );
cout << v[1] << endl;
}
How about simulating the "one past the last element" idiom?
You could also "terminate" the array with an empty "".

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

int main()
{
std::string s[] = { "aaa", "bbb", "ccc" };
std::vector< std::string vs(&s[0], &s[3]);

std::copy( vs.begin(),
vs.end(),
std::ostream_iterator< std::string >( std::cout, "\n") );
}

Oct 30 '06 #4

Gary Wessle wrote in message ...
>"Alf P. Steinbach" <al***@start.nowrites:
>* Gary Wessle:
whats an efficient way to copy a string[] to a vector<string>?
how about this?
#include <iostream>
#include <string>
#include <vector>
Using namespace std;
int main(){
string s[] = { "aaa", "bbb", "ccc" };
vector<stringv( s.begin(), s.end() );
cout << v[1] << endl;
}

That won't compile.

yes, it did not compile.
// ------------
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
int main(){
std::string B[]={"001","2","we","the world"};
// std::vector<std::stringvBstr;
// std::copy( B, B+(sizeof B/sizeof *B), std::back_inserter( vBstr ) );
std::vector<std::stringvBstr( B, B+(sizeof B/sizeof *B) );

for( size_t i(0); i < vBstr.size(); ++i ){
std::cout << vBstr.at( i ) <<std::endl;
}
} // main()
// ------------

--
Bob R
POVrookie
Oct 30 '06 #5

Gary Wessle schrieb:
whats an efficient way to copy a string[] to a vector<string>?

how about this?

#include <iostream>
#include <string>
#include <vector>

Using namespace std;
The using keyword does not start with a capital 'U'.
int main(){
string s[] = { "aaa", "bbb", "ccc" };
vector<stringv( s.begin(), s.end() );
Arrays do not have any member functions, so s.begin() and s.end() don't
work.

Since pointers can be used as iterators you can write

vector<stringv(s, s + 3);

hth
ralph

Oct 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
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...
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: Minkoo Seo | last post by:
Hi list. I'd like to print a line "" given a vector<string> containing "A", "BC", "D", "EF". The following code is what I've written for this purpose: #include <iostream> #include <vector>
5
by: Fred | last post by:
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...
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 *...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.