Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The differences between a string and an element of a vector<string> [duplicate]

Tags:

c++

string

vector

I'm a newcomer to programming. I am learning vector in C++. I am curious about why string s = 42; causes error but

vector<string>vec(3);
vec[0] = 42;

does not. Thank you!

like image 505
lightweight Avatar asked Dec 05 '22 13:12

lightweight


1 Answers

std::vector has nothing to do with that, your sample with std::vector is similar to

std::string s;
s = 42;

but

std::string s = 42; // Constructor: "equivalent" to std::string s = std::string(42)

is different than

std::string s;
s = 42; // assignation: s.operator =(42)

and std::string::operator=(char) exists whereas constructor taking char doesn't.

like image 155
Jarod42 Avatar answered Feb 16 '23 03:02

Jarod42