469,647 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

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 3192
* 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
By using this site, you agree to our Privacy Policy and Terms of Use.