模板有函数模板,类模板
函数模板
template<class T>
T Max(T a, T b)
{
return a > b ? a : b;
}
void main()
{
cout << Max(3, 26) << endl;
cout << Max('b', 'y') << endl;
}
类模板
template<class T>
class A
{
public:
A(T i=0):m_i(i){}
void print() { cout << m_i << endl; }
private:
T m_i;
};
void main()
{
A<int>a(10);
a.print();
}
STL主要部件:容器 算法 迭代器,迭代器把数据传给算法操作
begin--指向容器第一个元素的迭代器
end--指向最后一个元素的下一个迭代器
front--返回第一个元素
back--返回最后一个元素
assign--对vector中元素赋值
at--返回指定位置元素值
resize--改变元素数量大小
reserve--设置vector最小元素容量大小
clear--清空
empty--空
size--大小
capacity--容量
insert--插入
erase--删除指定元素
push_back--尾插
pop_back--尾删
swap--交换两个vector
用vector创建动态数组
void main()
{
vector<vector<char>>vv;
int i, j;
for (int i = 0; i < 3; i++)
{
vv.push_back(vector<char>());
for (j = 0; j < 4; j++)
{
vv[i].push_back('6');
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
cout << vv[i][j] << " ";
}
cout << endl;
}
}
迭代器可以像指针一样使用
void main()
{
int arr[5] = { 1,2,3,4,5 };
vector<int> v1(arr, arr + 5);
vector<int> v2;
vector<int> v3(v1);
int i;
for (i = 0; i < v1.size(); i++)
{
cout << v1[i] << " " << v1.at(i) << endl;
}
v2 = v3;//vector中重载了赋值“=”
vector<int>::iterator iter;//定义一个迭代器
for (iter = v2.begin(); iter != v2.end(); iter++)//begin返回迭代器
{
cout << *iter << " ";
}
cout << endl;
}
改变元素容量大小可能会把数据弄丢
void main()
{
int num = 0;
int i;
int* p = nullptr;
vector<int> v;
vector<int>::iterator iter;
v.reserve(10);
for (i = 1; i < 20; i++)
v.push_back(i);
v.resize(5);
cout << v.size() << endl;
cout << v.capacity() << endl;
for (iter = v.begin(); iter != v.end(); iter++)
cout << *iter << " ";
cout << endl;
v.resize(10);
for (iter = v.begin(); iter != v.end(); iter++)
cout << *iter << " ";
cout << endl;
}
void main()
{
int arr[5] = { 1,2,3,4,5 };
vector<int> v(arr,arr+3); //1 2 3
vector<int> v1(arr, arr + 5);
v.assign(5,3);//5个3
vector<int>::iterator iter;
for (iter = v.begin(); iter != v.end(); iter++)//begin返回迭代器
{
cout << *iter << " ";
}
cout << endl;
v.assign(v1.begin(), v1.begin()+3);//1 2 3
v1 = v;
}
void main()
{
int arr[5] = { 1,2,3,4,5 };
vector<int> v(arr, arr + 3);
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
// v.clear();
v.insert(v.begin(), 100);//100 1 2 3
v.insert(v.begin() + 2, arr, arr + 4);
//100 1 1 2 3 4 2 3
v.insert(v.begin(), v1.begin(), v1.end());
//10 20 30 100 1 1 2 3 4 2 40 40 40 3
v.insert(v.end() - 1, 3, 40);
vector<int> ::iterator iters = v.begin();
vector<int> ::iterator itere = v.end();
for (; iters != itere; iters++)
cout << *iters << " ";
cout << endl;
cout << "front :" << v.front() << endl;//返回第一个元素
cout << "back :" << v.back() << endl;//返回最后一个元素
//删除
v.pop_back();
cout << "size = " << v.size() << endl;
20 30 100 1 1 2 3 4 2 40 40 40
v.erase(v.begin());//删除第一个
v.erase(v.begin(), v.begin() + 3);//删除前三个
iters = v.begin();
itere = v.end();
for (; iters != itere; iters++)
cout << *iters << " ";
cout << endl;
}
void main()
{
//string类型转换为char*类型的字符串
string s = "wangpangpang";
const char* str = s.c_str();//c_str返回当前字符串首字符地址
cout << str << endl;
const char* str2 = "hello";
string s2 = str2; //string s2(str2)
s2 = "world";
cout << s2 << endl;
s2.assign("sssss");
cout << s2 << endl;
s2[3] = 'k';
cout << s2 << endl;
s2.at(2) = 'p';
cout << s2 << endl;
s2 = s2 + "tttt";
cout << s2 << endl;
s2.append("kkkkk");//追加
cout << s2 << endl;
}
void main()
{
string s = "abcdefg";
string s1 = "abcf";
string sub = "bd";
/* int pos = s.find(sub);//返回第一个sub的位置
cout << pos << endl;
string sub2 = "ef";
pos = s.find(sub2);
cout << pos << endl;
pos = s.rfind(sub2);//rfind()返回字符串最后一个次出现的位置
cout << pos << endl;*/
int ret = s1.compare(s);
cout << ret << endl;
string s2 = s.substr(1, 4); //偏移量 数量
cout << s2 << endl;
s2.insert(0, "kkk");
cout << s2 << endl;
s2.erase(0, 5);
cout << s2 << endl;
}
find():返回第一个出现的位置
rfind():返回最后一个出现的位置
compare():s1>s2返回正数,s1=s2返回0,s1<s2返回负数
substr():返回字符串的一部分