1、数组 array
数组array是固定大小的容器,类似于C语言中的数组,但提供了更多功能和安全性。其大小在编译时确定,不能更改。
#include <iostream>
#include <array>
#include <algorithm> // 用于std::sort等算法
#include <iterator> // 用于std::begin和std::end
using namespace std;
int main(int argc, char* argv[])
{
// 定义包含5个整数的array1
array<int, 5> array1 = {5, 2, 9, 1, 5};
// size()
cout << "Size of array: " << array1.size() << endl;
// max_size()
cout << "Max size of array: " << array1.max_size() << endl;
// empty()
cout << "Is array empty? " << (array1.empty() ? "Yes" : "No") << endl;
// at()
try
{
cout << "Element at index 2: " << array1.at(2) << endl;
cout << "Element at index 10: " << array1.at(10) << endl; // 超出范围,会抛出异常
}
catch (const std::out_of_range& e)
{
cout << "Error: " << e.what() << endl;
}
// operator []
cout << "Element at index 3: " << array1[3] << endl;
// front()
cout << "First element: " << array1.front() << endl;
// back()
cout << "Last element: " << array1.back() << endl;
// data()
cout << "Data pointer: " << array1.data() << endl;
// fill()
array1.fill(7);
cout << "Array after filling with 7: ";
for (const auto& elem : array1)
{
cout << elem << " ";
}
cout << endl;
// swap()
array<int, 5> array2 = {10, 30, 20, 40, 50};
array1.swap(array2);
cout << "Array after swapping with arr2: ";
for (const auto& elem : array1)
{
cout << elem << " ";
}
cout << endl;
// sort()
sort(array1.begin(), array1.end());
cout << "Array after sorting: ";
for (const auto& elem : array1)
{
cout << elem << " ";
}
cout << endl;
// reverse()
reverse(array1.begin(), array1.end());
cout << "Array after reversing: ";
for (const auto& elem : array1)
{
cout << elem << " ";
}
cout << endl;
return 0;
}
| xuanmiao@linux:~/Test/C++$ ./test Size of array: 5 Max size of array: 5 Is array empty? No Element at index 2: 9 Element at index 10: Error: array::at: __n (which is 10) >= _Nm (which is 5) Element at index 3: 1 First element: 5 Last element: 5 Data pointer: 0x7fff27e82440 Array after filling with 7: 7 7 7 7 7 Array after swapping with arr2: 10 30 20 40 50 Array after sorting: 10 20 30 40 50 Array after reversing: 50 40 30 20 10 |
2、向量 vector
向量vector是动态数组,其大小可以在运行时动态调整。它提供了自动内存管理,能够根据需要自动扩展或收缩。
#include <iostream>
#include <vector>
#include <algorithm> // 用于 sort 和 reverse
#include <iterator> // 用于 ostream_iterator
using namespace std;
int main()
{
// 创建一个空的 vector
vector<int> vec;
// size()
cout << "Initial size: " << vec.size() << endl;
// capacity()
cout << "Initial capacity: " << vec.capacity() << endl;
// empty()
cout << "Is vector empty? " << (vec.empty() ? "Yes" : "No") << endl;
// push_back()
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
cout << "After push_back: ";
for (int i : vec) {cout << i << " ";}
cout << endl;
// pop_back()
vec.pop_back();
cout << "After pop_back: ";
for (int i : vec) {cout << i << " ";}
cout << endl;
// front()
cout << "First element: " << vec.front() << endl;
// back()
cout << "Last element: " << vec.back() << endl;
// at()
try
{
cout << "Element at index 1: " << vec.at(1) << endl;
cout << "Element at index 10: " << vec.at(10) << endl; // 超出范围,会抛出异常
}
catch (const out_of_range& e)
{
cout << "Error: " << e.what() << endl;
}
// operator[]
cout << "Element at index 0: " << vec[0] << endl;
// insert()
vec.insert(vec.begin() + 1, 15);
cout << "After insert: ";
for (int i : vec) {cout << i << " ";}
cout << endl;
// erase()
vec.erase(vec.begin() + 1);
cout << "After erase: ";
for (int i : vec) {cout << i << " ";}
cout << endl;
// clear()
vec.clear();
cout << "After clear: ";
cout << "Size: " << vec.size() << ", Capacity: " << vec.capacity() << endl;
// resize()
vec.resize(5, 100);
cout << "After resize to 5 with value 100: ";
for (int i : vec) {cout << i << " ";}
cout << endl;
// reserve()
vec.reserve(10);
cout << "After reserve 10: ";
cout << "Size: " << vec.size() << ", Capacity: " << vec.capacity() << endl;
// assign()
vec.assign(3, 200);
cout << "After assign 3 with value 200: ";
for (int i : vec) {cout << i << " ";}
cout << endl;
// sort()
sort(vec.begin(), vec.end());
cout << "After sort: ";
for (int i : vec) {cout << i << " ";}
cout << endl;
// reverse()
reverse(vec.begin(), vec.end());
cout << "After reverse: ";
for (int i : vec) {cout << i << " ";}
cout << endl;
// swap()
vector<int> vec2 = {1, 2, 3};
vec.swap(vec2);
cout << "After swap with vec2: ";
for (int i : vec) {cout << i << " ";}
cout << endl;
// data()
int* dataPtr = vec.data();
cout << "Data pointer: " << dataPtr << endl;
return 0;
}
| xuanmiao@linux:~/Test/C++$ ./test Initial size: 0 Initial capacity: 0 Is vector empty? Yes After push_back: 10 20 30 After pop_back: 10 20 First element: 10 Last element: 20 Element at index 1: 20 Element at index 10: Error: vector::_M_range_check: __n (which is 10) >= this->size() (which is 2) Element at index 0: 10 After insert: 10 15 20 After erase: 10 20 After clear: Size: 0, Capacity: 4 After resize to 5 with value 100: 100 100 100 100 100 After reserve 10: Size: 5, Capacity: 10 After assign 3 with value 200: 200 200 200 After sort: 200 200 200 After reverse: 200 200 200 After swap with vec2: 1 2 3 Data pointer: 0x5624c8af22e0 |
3、双端队列 deque
#include <iostream>
#include <deque>
#include <algorithm> // 用于sort等算法
using namespace std;
int main(int argc, char* argv[])
{
// 创建一个空的 deque
deque<int> dq;
// size()
cout << "Initial size: " << dq.size() << endl;
// empty()
cout << "Is deque empty? " << (dq.empty() ? "Yes" : "No") << endl;
// push_back()
dq.push_back(10);
dq.push_back(20);
dq.push_back(30);
cout << "After push_back: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
// push_front()
dq.push_front(5);
dq.push_front(0);
cout << "After push_front: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
// pop_back()
dq.pop_back();
cout << "After pop_back: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
// pop_front()
dq.pop_front();
cout << "After pop_front: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
// front()
cout << "First element: " << dq.front() << endl;
// back()
cout << "Last element: " << dq.back() << endl;
// at()
try {
cout << "Element at index 1: " << dq.at(1) << endl;
cout << "Element at index 10: " << dq.at(10) << endl; // 超出范围,会抛出异常
}
catch (const out_of_range& e)
{
cout << "Error: " << e.what() << endl;
}
// operator[]
cout << "Element at index 0: " << dq[0] << endl;
// insert()
auto it = dq.begin() + 1;
dq.insert(it, 15);
cout << "After insert 15 at index 1: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
// erase()
dq.erase(it);
cout << "After erase element at index 1: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
// clear()
dq.clear();
cout << "After clear: ";
cout << "Size: " << dq.size() << endl;
// resize()
dq.resize(5, 100);
cout << "After resize to 5 with value 100: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
// assign()
dq.assign(3, 200);
cout << "After assign 3 with value 200: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
// sort()
sort(dq.begin(), dq.end());
cout << "After sort: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
// swap()
deque<int> dq2 = {1, 2, 3};
dq.swap(dq2);
cout << "After swap with dq2: ";
for (int i : dq) { cout << i << " "; }
cout << endl;
return 0;
}
| xuanmiao@linux:~/Test/C++$ ./test Initial size: 0 Is deque empty? Yes After push_back: 10 20 30 After push_front: 0 5 10 20 30 After pop_back: 0 5 10 20 After pop_front: 5 10 20 First element: 5 Last element: 20 Element at index 1: 10 Element at index 10: Error: deque::_M_range_check: __n (which is 10)>= this->size() (which is 3) Element at index 0: 5 After insert 15 at index 1: 5 15 10 20 After erase element at index 1: 5 10 20 After clear: Size: 0 After resize to 5 with value 100: 100 100 100 100 100 After assign 3 with value 200: 200 200 200 |
4、双向链表 list
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
// 创建一个空的 list
list<int> lst;
// size()
cout << "Initial size: " << lst.size() << endl;
// empty()
cout << "Is list empty? " << (lst.empty() ? "Yes" : "No") << endl;
// push_back()
lst.push_back(10);
lst.push_back(20);
lst.push_back(30);
cout << "After push_back: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// push_front()
lst.push_front(5);
lst.push_front(0);
cout << "After push_front: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// pop_back()
lst.pop_back();
cout << "After pop_back: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// pop_front()
lst.pop_front();
cout << "After pop_front: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// front()
cout << "First element: " << lst.front() << endl;
// back()
cout << "Last element: " << lst.back() << endl;
// insert()
auto it = lst.begin(); // 在第一个元素的位置插入
advance(it, 1); // 移动到第二个位置
lst.insert(it, 15);
cout << "After insert 15 at second position: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// erase()
lst.erase(it);
cout << "After erase element at second position: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// clear()
lst.clear();
cout << "After clear: ";
cout << "Size: " << lst.size() << endl;
// resize()
lst.resize(5, 100);
cout << "After resize to 5 with value 100: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// assign()
lst.assign(3, 200);
cout << "After assign 3 with value 200: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// sort()
lst.sort();
cout << "After sort: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// reverse()
lst.reverse();
cout << "After reverse: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// swap()
list<int> lst2 = {1, 2, 3};
lst.swap(lst2);
cout << "After swap with lst2: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// remove()
lst.remove(2);
cout << "After remove value 2: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
// unique()
lst.push_back(1);
lst.push_back(1);
cout << "Before unique: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
lst.unique();
cout << "After unique: ";
for (int i : lst) { cout << i << " ";}
cout << endl;
return 0;
}
| xuanmiao@linux:~/Test/C++$ ./test Initial size: 0 Is list empty? Yes After push_back: 10 20 30 After push_front: 0 5 10 20 30 After pop_back: 0 5 10 20 After pop_front: 5 10 20 First element: 5 Last element: 20 After insert 15 at second position: 5 15 10 20 After erase element at second position: 5 15 20 After clear: Size: 0 After resize to 5 with value 100: 100 100 100 100 100 After assign 3 with value 200: 200 200 200 After sort: 200 200 200 After reverse: 200 200 200 After swap with lst2: 1 2 3 After remove value 2: 1 3 Before unique: 1 3 1 1 After unique: 1 3 1 |
5、字符串 string
#include <iostream>
#include <string>
using namespace std;
int main() {
// 创建一个空的 string
string str;
// size() 和 length():获取字符串的长度
cout << "Initial size: " << str.size() << endl;
cout << "Initial length: " << str.length() << endl;
// empty():检查字符串是否为空
cout << "Is string empty? " << (str.empty() ? "Yes" : "No") << endl;
// assign():用新内容重新分配字符串
str.assign("Hello, World!");
cout << "After assign: " << str << endl;
// push_back():在字符串末尾添加一个字符
str.push_back('!');
cout << "After push_back: " << str << endl;
// pop_back():移除字符串的最后一个字符
str.pop_back();
cout << "After pop_back: " << str << endl;
// front() 和 back():访问第一个和最后一个字符
cout << "First character: " << str.front() << endl;
cout << "Last character: " << str.back() << endl;
// at() 和 operator[]:通过索引访问字符
cout << "Character at index 1: " << str.at(1) << endl;
cout << "Character at index 0: " << str[0] << endl;
// insert():在指定位置插入字符串
str.insert(7, "C++ ");
cout << "After insert: " << str << endl;
// erase():移除指定位置的字符
str.erase(7, 3);
cout << "After erase: " << str << endl;
// replace():替换指定范围的字符
str.replace(7, 5, "C++");
cout << "After replace: " << str << endl;
// clear():清空字符串
str.clear();
cout << "After clear: " << str << endl;
// resize():调整字符串的大小
str.resize(10, 'x');
cout << "After resize to 10 with 'x': " << str << endl;
// substr():获取子字符串
str = "Hello, World!";
string sub = str.substr(7, 5);
cout << "Substring from index 7, length 5: " << sub << endl;
// find():查找子字符串的位置
size_t pos = str.find("World");
if (pos != string::npos) {
cout << "Found 'World' at position: " << pos << endl;
} else {
cout << "Substring 'World' not found." << endl;
}
// rfind():从后向前查找子字符串的位置
pos = str.rfind("o");
if (pos != string::npos) {
cout << "Last occurrence of 'o' at position: " << pos << endl;
} else {
cout << "Character 'o' not found." << endl;
}
// compare():比较两个字符串
string str2 = "Hello, C++";
int cmp = str.compare(str2);
if (cmp == 0) {
cout << "Strings are equal." << endl;
} else if (cmp < 0) {
cout << "First string is less than the second." << endl;
} else {
cout << "First string is greater than the second." << endl;
}
// c_str() 和 data():获取 C 风格的字符串
const char* cstr = str.c_str();
cout << "C-style string: " << cstr << endl;
// swap():交换两个字符串的内容
str.swap(str2);
cout << "After swap: " << str << " and " << str2 << endl;
// append() 和 +=:追加字符串
str.append(" and C++");
cout << "After append: " << str << endl;
// operator+:连接字符串
string str3 = str + " is great!";
cout << "Concatenated string: " << str3 << endl;
// to_string():将数值转换为字符串
int num = 42;
string numStr = to_string(num);
cout << "Number to string: " << numStr << endl;
return 0;
}
| xuanmiao@linux:~/Test/C++$ ./test Initial size: 0 Initial length: 0 Is string empty? Yes After assign: Hello, World! After push_back: Hello, World!! After pop_back: Hello, World! First character: H Last character: ! Character at index 1: e Character at index 0: H After insert: Hello, C++ World! After erase: Hello, World! After replace: Hello, C++d! After clear: After resize to 10 with 'x': xxxxxxxxxx Substring from index 7, length 5: World Found 'World' at position: 7 Last occurrence of 'o' at position: 8 First string is greater than the second. C-style string: Hello, World! After swap: Hello, C++ and Hello, World! After append: Hello, C++ and C++ Concatenated string: Hello, C++ and C++ is great! Number to string: 42 |
序列容器
浙公网安备 33010602011771号