Initialization
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec(10, -1);
vector<int> ivec2(ivec);
for (auto x : ivec2) {cout << x << endl;}
vector<int> jvec(10); //initialized as 0s;
for (auto x: jvec)
cout << x << endl;
vector<int> zvec{1,2,3};
vector<int> avec={1,2,3}; //zvec and avec are equivalent
for (int i=0; i<zvec.size(); i++)
{
std::cout << (zvec[i] == avec[i]) << endl;
}
return 0;
}
Methods
v.empty()
: returntrue
if v is emptyv.size
: return the number of elements in vv.push_back(t)
: add an element to v.v1==v2
: returntrue
if number and values are equal
int main()
{
vector<int> avec{1,2,3};
vector<int> bvec{1,2,3};
cout << (avec==bvec) << endl;
cout << (&avec==&bvec) << endl;
return 0;
}
1
0
[Finished in 0.9s]
Iterator
int main()
{
string s("hello");
if (s.begin()!=s.end())
{
auto it = s.begin();
*it = toupper(*it);
}
cout << s << endl;
for (auto it = s.begin(); it !=s.end() && !isspace(*it); ++it)
{
*it = toupper(*it);
}
cout << s << endl;
return 0;
}
Hello
HELLO
[Finished in 0.8s]
vector<int>::iterator it
vector<int>::const_iterator it2
: it2 can read but not write