C++ std::vector | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

C++ std::vector

Hi, I've a std::vector<std::string> vec1 which has 8 elements (thanks push_back) I've another std::vector<std;;string> vec2, wich also has 8 elements. I want to copy all vec2 elements on vec1, to refresh the data of vec1 how ?

21st Mar 2019, 8:29 AM
NoxFly
NoxFly - avatar
17 Answers
+ 14
You can easily to that. vec1.clear() // Clears the vector. vec1.swap(vec2) // Copies the content.
21st Mar 2019, 8:46 AM
Letsintegreat
Letsintegreat - avatar
22nd Mar 2019, 12:19 AM
NoxFly
NoxFly - avatar
+ 4
Boy lr girl nurse ? (Good field !)
22nd Mar 2019, 12:42 AM
NoxFly
NoxFly - avatar
+ 4
I'm on programming school in university
22nd Mar 2019, 12:44 AM
NoxFly
NoxFly - avatar
+ 3
😄 ... please believe me. It was just coincidence. I am not in your head. I will answer you after work. My boss seems not amused.
22nd Mar 2019, 12:30 AM
Nulpe
+ 3
Hahaha ok no problem ^^ you work in what ?
22nd Mar 2019, 12:33 AM
NoxFly
NoxFly - avatar
+ 2
Nulpe thx I'll show you a code (that actually doesn't work) like that you'll know how I wanted to do
22nd Mar 2019, 12:10 AM
NoxFly
NoxFly - avatar
+ 2
I didn't waiting for girl nurse haha, just to be curious ans know you a bit ^^
22nd Mar 2019, 12:44 AM
NoxFly
NoxFly - avatar
+ 1
If you want to define your vector in a class, I have an example for you. 😁 https://code.sololearn.com/c25v4J4CiyHQ/?ref=app
22nd Mar 2019, 12:07 AM
Nulpe
+ 1
NoxFly a simple assignment is enough vec1=vec2;
5th Jun 2020, 7:04 PM
Anthony Maina
Anthony Maina - avatar
0
Its also posible to set your String-Vector with the constructor.
22nd Mar 2019, 12:09 AM
Nulpe
0
I work as nurse and i am a student at a technical university
22nd Mar 2019, 12:41 AM
Nulpe
0
Boy nurse ... Sorry ^^
22nd Mar 2019, 12:43 AM
Nulpe
0
Here to learn programming
22nd Mar 2019, 4:53 AM
okoh victory
okoh victory - avatar
0
NoxFly ドリアン I believe "cout<<testVec" is in C++ not valid. You need a loop to print every element of an array or vector,.
22nd Mar 2019, 4:58 AM
Nulpe
0
okoh victory you are welcome ... I am a noob 😁
22nd Mar 2019, 4:59 AM
Nulpe
0
You can also use move semantics Vec1.assign(move(vec2));
22nd Jul 2020, 10:42 AM
Anthony Maina
Anthony Maina - avatar