vector
resize
增大或缩小容器实际存储的元素的数量
resize只会改变实际存储的元素的数量,并不会改变容器的最大容量,即容器的capacity
1、如果resize指定的值小于当前容器的实际存储的元素的数量,则多出来的元素都会被删除(这里的删除是逻辑上的删除,因为还可以访问到)
2、如果大于当前容器的实际存储的元素数量,则会添加新的元素进行补充,按照指定的值或者容器进行值初始化
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
cout<<"size: "<<v.size()<<endl; // 5
v.resize(1);
cout<<"has delete:"<<v[1]<<endl; // 2
v.push_back(9);
cout<<"new value:"<<v[1]<<endl; // 9
cout<<"size: "<<v.size()<<endl; // 2
v.resize(8); // 超过当前元素个数,分配默认值为0
cout<<"Default initialization:"<<v[7]<<endl; // 0
cout<<"size: "<<v.size()<<endl; // 8
v.resize(11,1); // 超过当前元素个数,设置了默认值为1
cout<<"designated value:"<<v[10]<<endl; // 1
cout<<"size: "<<v.size()<<endl; // 11
return 0;
}
reserve
只适用于vector和string。
reserve(n):分配至少能容纳n个元素的内存空间,reserve并不改变容器中元素的数量,它仅影响vector预先分配多大的内存空间,即改变的是capacity。
1、只有当 n 大于当前的容量时,才会改变容器的容量,reserve至少分配与需求一样大的内存空间,也可能更大
2、当n小于或者等于当前容量时,reserve什么也不做,容器也不会退回内存空间
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
cout<<"size: "<<v.size()<<endl; // 5
cout<<"capacity: "<<v.capacity()<<endl; // 5
v.reserve(8);
cout<<"size: "<<v.size()<<endl; // 5
cout<<"capacity: "<<v.capacity()<<endl; // 8
v.reserve(3); // 3小于8,不会改变容量大小
cout<<"size: "<<v.size()<<endl; // 5
cout<<"capacity: "<<v.capacity()<<endl; // 8
return 0;
}
shrink_to_fit
只适用于vector,string和deque
退回不需要的内存空间,将capacity减少为与size相同大小
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
cout<<"size: "<<v.size()<<endl; // 5
cout<<"capacity: "<<v.capacity()<<endl; // 5
v.push_back(6);
cout<<"size: "<<v.size()<<endl;// 6
cout<<"capacity: "<<v.capacity()<<endl; // 10
v.shrink_to_fit(); // 退回没有使用的空间,容量和size大小一致
cout<<"size: "<<v.size()<<endl; // 6
cout<<"capacity: "<<v.capacity()<<endl; // 6
return 0;
}
set_difference和inserter
1、set_difference
是C++中的一个函数,它接受两个有序的范围作为输入,并输出在第一个范围中但不在第二个范围中的元素。输出写入到一个第三个范围中,该范围必须足够大以容纳所有输出的元素。
也就是求出集合A和集合B中,只在集合A中而不在集合B中的元素
使用之前需要排序
2、inserter(container,pos):返回通用插入型迭代器,内部会调用容器container的insert(pos)方法将数据插入到pos位置
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v1{1,2,3,4,5};
vector<int> v2{1,2,6};
vector<int> v3;
// 相当于把在v1而不再v2的元素插入到v3后面
std::set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),inserter(v3,v3.end()));
// 输出 3,4,5
for(auto &v:v3) {
cout << v <<endl;
}
return 0;
}
std::find
查找元素
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
if(find(arr.begin(), arr.end(), 3) != arr.end()) {
cout << "vector已找到" << endl;
}
return 0;
}
string
substr
截取子串test,第一个参数为起始位置,第二个参数为截取长度
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
string str("test test2");
string str1 = str.substr(0, 4);
string str2 = str.substr(0, find(str.begin(), str.end(), ' ') - str.begin());
string str3 = str.substr(0, str.find(' '));
cout << str1 << str2 << str3 << endl;
return 0;
}
find
字符串查找,返回的不是迭代器,是索引,没有找到的话,返回string::npos
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
string str("test test2");
cout<< str.find(" ") << endl; // 4
if(str.find("1") == string::npos) {
cout<<"没有找到"<<endl;
}
return 0;
}
tuple
tuple是一组有序的值。在关系型数据库中,某个table的一个row也可以称为tuple;除此之外,tuple也可以让函数用来返回2个以上的返回值
初始化、tie解包、查询、元组大小、元素数据类型操作如下
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
// 初始化
tuple<bool, int, double, string> obj(true, 1, 3.0, "test");
tuple<bool, int, double, string> obj2{false, 2, 4.0, "test2"};
tuple<bool, int, double, string> obj3 = make_tuple(true, 3, 5.0, "test3");
// tie解包操作
bool myBool;
int myInt;
double myDouble;
string myString;
tie(myBool, myInt, myDouble, myString) = obj;
cout << myDouble << endl; // 3.0
// 获取元素
cout<< get<2>(obj) << endl; // 3.0
// 获取元组大小
cout<<tuple_size<tuple<bool, int, double, string>>::value<<endl;
cout<<tuple_size<decltype(obj)>::value<<endl; // 4
// 获取元素的数据类型
tuple_element<1, decltype(obj)>::type temp;
temp=get<1>(obj);
cout<<temp<<endl; // 1
return 0;
}