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;
} 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?
"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.
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") );
}
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
?
//...
|
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>
|
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...
|
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!...
|
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
*...
|
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",...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |