copy string[] to vector<string>

G

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<string> v( s.begin(), s.end() );
cout << v[1] << endl;
}
 
A

Alf P. Steinbach

* 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<string> v( s.begin(), s.end() );
cout << v[1] << endl;
}

That won't compile.
 
G

Gary Wessle

Alf P. Steinbach said:
* 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<string> v( s.begin(), s.end() );
cout << v[1] << endl;
}

That won't compile.

yes, it did not compile.
 
S

Salt_Peter

Gary said:
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<string> v( 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::eek:stream_iterator< std::string >( std::cout, "\n") );
}
 
B

BobR

Gary Wessle wrote in message ...
Alf P. Steinbach said:
* 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<string> v( 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::string> vBstr;
// std::copy( B, B+(sizeof B/sizeof *B), std::back_inserter( vBstr ) );
std::vector<std::string> vBstr( B, B+(sizeof B/sizeof *B) );

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

ralph

Gary said:
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<string> v( 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<string> v(s, s + 3);

hth
ralph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top