文章目录
Vector
vector和字符串都是cpp自建的数据结构,和数组有关。
与数组相比,vector更具易用性。
vector是使用最广泛的序列容器。
int main()
{
int a[3];
int b[3] = a; // 系统报错,系统不支持复制
// error: array initializer must be an initializer list
std::vector<int> x;
std::vector<int> y;
// x 和 y 的数据类型是不一样的
y = x; // vector支持复制
}
数组的个数,是在编译期就确定的。运行期只能是有x0,x1,x2。但是vector定义了,它的元素个数是0个。我们可以向vector插入或者删除元素,在运行期。
但是vector的性能比数组差很多,在运行期动态改变元素个数,也会有额外的开销。所以vector侧重易用性,数组侧重性能。
int main()
{
int a[3]; // 包含3个元素
std::vector<int> x; // 包含0个元素
int b[3] = {1, 2, 3}; // 聚合初始化数组
std::vector<int> y = {4, 5, 6}; // 聚合初始化vector
}
数组不能使用auto来声明。
int main()
{
std::vector<int> x(3); // x包含3个元素,每个都默认是0
std::vector<int> y(3, 1); // x包含3个元素,每个都默认是1
for (auto i : y)
{
cout << i << endl;
}
}
int main()
{
std::vector<int> x(3); // x包含3个元素,每个都默认是0
cout << x.size() << endl; // 3 x是vector,size是veoctor类模版定义的函数,也叫x的方法
cout << x.empty() << endl; // x是否为空,空输出1,非空输出0
x.push_back(2); // vector添加元素
for (auto i : x)
{
cout << i << endl;
}
x.pop_back(); // vector最后一个元素pop
for (auto i : x)
{
cout << i;
}
}
int main()
{
std::vector<int> x = {1, 2, 3};
std::vector<int> y = {1, 3, 4};
cout << (x == y) << endl; // 0 vector支持比较操作,字典序比较
cout << (x < y) << endl; // 1
}
int main()
{
std::vector<int> x = {1, 2, 3};
// 如果越界了,有的编译器可能会报错,可能不报错
cout << x[2] << endl; // 输出3, vector的索引
// 如果越界了,一定会报错 out_of_range: vector
cout << x.at(2) << endl; // 输出3, vector访问索引的方法
}
int main()
{
std::vector<int> x = {1, 5, 9};
auto b = std::begin(x); // b不同于数组,不是一个指针,而是一个iterator迭代器
auto e = std::end(x);
for (auto val : x)
{
cout << val << ' ';
}
cout << endl;
auto b1 = x.begin(); // vector内置方法
auto e1 = x.end();
while (b1 != e1)
{
cout << *b1 << endl;
b1 += 1;
}
}
int main()
{
std::vector<int> x = {1, 5, 9};
auto b = std::begin(x); // b不同于数组,不是一个指针,而是一个iterator迭代器
auto e = std::end(x);
// b是一个随机访问的迭代器,有如下方法:
*b; // 解引用
b[1]; // 取索引
cout << e - b << endl; // 3 迭代器相减求距离
b = b + 1; // 移动
}
int main()
{
std::vector<int> x = {1, 5, 9};
auto b = std::begin(x);
auto e = std::end(x);
x.push_back(100);
while (b != e)
{
cout << *b << ' '; // 1, 5, 9 没有100。说明b和e失效了
b += 1;
}
}
int main()
{
int a[3][4]; // 2维数组
std::vector<std::vector<int>> x; // 2维vector
x.push_back(std::vector<int>());
x[0].push_back(100);
cout << x[0][0] << endl; // 100
}
int main()
{
int a[3][4]; // 2维数组
std::vector<std::vector<int>> x{{1, 2, 3}, {4, 5, 6}};
cout << x[1][2] << endl; // 6
int a[3][4]; // 2维数组
// vector不会自动补0,不同于数组。
std::vector<std::vector<int>> x{{1, 2, 3}, {4, 5}};
}
字符串 String
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string x = "Jammy";
std::string y = x;
y = y + " Best...";
cout << y << endl;
}
int main()
{
std::string x(3, 'a');
std::string y(x);
cout << y << endl; // aaa
}
int main()
{
std::string x("Hello world");
std::string y("Hello"); // y的初始化
cout << x.size() << endl; // 11
cout << (x == y) << endl; // 0
cout << (x > y) << endl; // 1
y = "Jammy !"; // y的赋值
y = y + x;
cout << y << endl; // Jammy !Hello world
// cout << "aaa" + "bbb" << endl; // 编译错误 不支持字符串这么相加
}
int main()
{
std::string x = "Jammy";
std::string y;
cout << y << endl; // ""
y = x + "Boy!";
cout << y << endl; // JammyBoy!
y = "Boy" + x;
cout << y << endl; // BoyJammy
// y = "Hello" + "world" + x; // 不合法,前面两个字符串不能直接相加
y = std::string("Hello") + "world" + x; // HelloworldJammy
cout << y << endl;
y = std::string("Hello") + "world" + " !"; // 只要保证开头是string即可
cout << y << endl; // Helloworld !
}
int main()
{
std::string x = "Jammy";
cout << x[2] << endl; // m 支持索引
}
int main()
{
std::string x = "Jammy";
auto ptr = x.c_str(); // string字符串与c内建字符串的转换(结尾\0占位符), ptr是一个指针
cout << ptr << endl; // Jammy,为什么要转换,可能借口接的是c内建的字符串
}