472,125 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 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 3370
* 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matt Garman | last post: by
5 posts views Thread by Minkoo Seo | last post: by
10 posts views Thread by Shafik | last post: by
6 posts views Thread by arnuld | last post: by
42 posts views Thread by barcaroller | last post: by
reply views Thread by leo001 | last post: by

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.