C++ 笔记 vector类
基础使用方法:
基础定义:
#include <vector>
using namespace std;
int main(){
vector <int> test1;
vector <vector <vector <int>>> test1;
vector <int &> test1;
}
初始化:
#include <vector>
using namespace std;
int main(){
vector <int> test1;
vector <int> test2;
test1.push_back(12);
test2 = test1;
return 0;
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/df15f2a71babfbdce6e91e7d79d97b3f.png)
带值进行初始化:
#include <vector>
using namespace std;
int main(){
vector <int> test1;
vector <int> test2;
test1.push_back(12);
vector <int> test3(test1);
test2 = test1;
vector <int> test4{1,2,3,4,5,6};
vector <int> test5(3);
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bb360656f3baf973963f497085786845.png)
注意:
#include <vector>
#include <string>
using namespace std;
int main(){
vector <string> str_test{10};
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1ef7bb8c5cf1281475de8f25df0af8a6.png)
批量生成元素:
#include <vector>
#include <string>
using namespace std;
int main(){
vector <string> str_test1{ 3,"hi c++" };
vector <string> str_test2(3,"hi C++");
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ebee4f113b3e0d5ed11844b09ba59719.png)
常用方法:
#include <vector>
#include <string>
using namespace std;
int main(){
vector <double> str_test1{10,1.1};\
str_test1.push_back(2.2);
str_test1.empty();
str_test1.size();
str_test1.clear();
str_test1[1];
str_test1[50];
return 0;
}