stl第一部分: string, vector, deque, stack
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
|
||||
// vector 的删除操作
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(vector<T> &v)
|
||||
{
|
||||
typename vector<T>::iterator it = v.begin();
|
||||
while (it != v.end())
|
||||
{
|
||||
cout << *it << " ";
|
||||
it++;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr1[5] = {1, 2, 3, 4, 5};
|
||||
vector<int> v1(arr1, arr1 + 5);
|
||||
print(v1);
|
||||
|
||||
vector<int>::iterator it = v1.begin();
|
||||
v1.insert(it, 1, 10); // 在 it 位置插入 1 个 10
|
||||
print(v1);
|
||||
|
||||
it = v1.begin(); // 重新获取迭代器,因为之前的迭代器已经失效(插入后迭代器首地址变了)
|
||||
v1.erase(it); // 删除 it 位置的元素
|
||||
print(v1);
|
||||
|
||||
it = v1.begin();
|
||||
v1.erase(it, it + 2); // 删除 [it, it+2) 位置的元素
|
||||
print(v1);
|
||||
|
||||
// 清空
|
||||
v1.clear();
|
||||
v1.empty(); // 判断是否为空
|
||||
cout << "清空后:" << endl;
|
||||
print(v1);
|
||||
cout << "size: " << v1.size() << " capacity: " << v1.capacity() << endl; // size: 0 capacity: 8
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
|
||||
// vector 的删除操作
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(vector<T> &v)
|
||||
{
|
||||
typename vector<T>::iterator it = v.begin();
|
||||
while (it != v.end())
|
||||
{
|
||||
cout << *it << " ";
|
||||
it++;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr1[5] = {1, 2, 3, 4, 5};
|
||||
vector<int> v1(arr1, arr1 + 5);
|
||||
print(v1);
|
||||
|
||||
vector<int>::iterator it = v1.begin();
|
||||
v1.insert(it, 1, 10); // 在 it 位置插入 1 个 10
|
||||
print(v1);
|
||||
|
||||
v1.resize(0); // 缩小
|
||||
vector<int>(v1).swap(v1); // 重新分配内存
|
||||
print(v1);
|
||||
cout << "size: " << v1.size() << " capacity: " << v1.capacity() << endl; // size: 2 capacity: 8
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// vector 的使用
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> v1;
|
||||
// int mSize = v1.size(); // 获取容器中元素的个数
|
||||
int mCapacity = v1.capacity(); // 获取容器的容量
|
||||
int cnt = 0; // 扩容次数
|
||||
cout << "初始容量: " << mCapacity << endl;
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
v1.push_back(i); // 向容器中添加元素
|
||||
// cout << "size = " << v1.size() << " capacity = " << v1.capacity() << endl;
|
||||
if (mCapacity != v1.capacity())
|
||||
{
|
||||
|
||||
mCapacity = v1.capacity();
|
||||
cout << "第" << ++cnt << "次扩容, "
|
||||
<< "当前容量为: " << v1.capacity() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4, 5, 9}; // 静态数组
|
||||
// vector<int> v1(arr, arr + 5); // 动态数组,通过静态数组初始化
|
||||
// vector<int> v1(arr, arr + sizeof(arr) / sizeof(arr[0])); // 动态数组,通过静态数组初始化
|
||||
|
||||
// 使用 assign 方法初始化
|
||||
vector<int> v1;
|
||||
v1.assign(arr, arr + sizeof(arr) / sizeof(arr[0]));
|
||||
|
||||
vector<int>::iterator it = v1.begin(); // 迭代器
|
||||
while (it != v1.end())
|
||||
{
|
||||
cout << *it << " ";
|
||||
it++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(vector<T> &v)
|
||||
{
|
||||
typename vector<T>::iterator it = v.begin();
|
||||
while (it != v.end())
|
||||
{
|
||||
cout << *it << " ";
|
||||
it++;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr1[5] = {1, 2, 3, 4, 5};
|
||||
vector<double> v1(arr1, arr1 + 5);
|
||||
vector<double> v2(5, 1.2f);
|
||||
v1.swap(v2);
|
||||
|
||||
print(v1);
|
||||
print(v2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr1[5] = {1, 2, 3, 4, 5};
|
||||
vector<double> v1(arr1, arr1 + 5);
|
||||
cout << "size: " << v1.size() << endl;
|
||||
cout << "capacity: " << v1.capacity() << endl;
|
||||
cout << "first addr: " << &v1[0] << endl;
|
||||
|
||||
v1.resize(20); // 重新指定容器的长度
|
||||
cout << "size: " << v1.size() << endl;
|
||||
cout << "capacity: " << v1.capacity() << endl;
|
||||
// 调整前后首地址会发生变化
|
||||
cout << "first addr: " << &v1[0] << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(vector<T> &v)
|
||||
{
|
||||
typename vector<T>::iterator it = v.begin();
|
||||
while (it != v.end())
|
||||
{
|
||||
cout << *it << " ";
|
||||
it++;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr1[5] = {1, 2, 3, 4, 5};
|
||||
vector<double> v1(arr1, arr1 + 5);
|
||||
|
||||
v1.resize(10, 9); // 重新指定容器的长度,并用9填充
|
||||
print(v1);
|
||||
|
||||
vector<int> v2;
|
||||
v2.reserve(3); // 预留足够的空间避免频繁扩容导致的首地址变化
|
||||
// v2.resize(2); // resize 也可以预留空间,但是不会填充
|
||||
v2.push_back(1);
|
||||
cout << "first addr: " << &v2[0] << endl;
|
||||
v2.push_back(2);
|
||||
cout << "first addr: " << &v2[0] << endl;
|
||||
v2.push_back(3);
|
||||
cout << "first addr: " << &v2[0] << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(vector<T> &v)
|
||||
{
|
||||
typename vector<T>::iterator it = v.begin();
|
||||
while (it != v.end())
|
||||
{
|
||||
cout << *it << " ";
|
||||
it++;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr1[5] = {1, 2, 3, 4, 5};
|
||||
vector<double> v1(arr1, arr1 + 5);
|
||||
|
||||
print(v1);
|
||||
|
||||
cout << "first element is: " << v1.front() << endl;
|
||||
cout << "first element addr is: " << &v1.front() << endl; // 与 day9/d7.cpp 中的 first addr 不同
|
||||
cout << "last element is: " << v1.back() << endl;
|
||||
cout << "last element addr is: " << &v1.back() << endl; // 与 day9/d7.cpp 中的 first addr 不同
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
|
||||
// vector 的插入操作
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(vector<T> &v)
|
||||
{
|
||||
typename vector<T>::iterator it = v.begin();
|
||||
while (it != v.end())
|
||||
{
|
||||
cout << *it << " ";
|
||||
it++;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr1[5] = {1, 2, 3, 4, 5};
|
||||
vector<int> v1(arr1, arr1 + 5);
|
||||
print(v1);
|
||||
|
||||
vector<int>::iterator it = v1.begin();
|
||||
v1.insert(it, 1, 10); // 在 it 位置插入 1 个 10
|
||||
print(v1);
|
||||
|
||||
vector<int>::iterator it2 = v1.begin();
|
||||
v1.insert(it2, 2, 9); // 在 it 位置插入 2 个 9,之前的元素会向后移动
|
||||
print(v1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user