#include<iostream>
#include<string>
#include<vector>
using namespace std;
void test() {
//string对象的定义和初始化
string s1;
string s2 = s1;
string s3 = "hiya";
string s4(10, 'c');
int a = s1.size();
if (!s2.empty()) {
cout << "" << endl;
}
//使用getline读取一整行
string line;
while (getline(cin, line)) {
cout << line << endl;
}
//字面值与string对象相加
string s1 = "hello";
string s2 = "world";
string s3 = s1 + ',' + s2;
//string s5 = "hello" + ","; 错误两个运算对象都不是string
string s4 = s1 + ","; //两个运算对象必须含有一个string
//用for循环处理每一个字符
string str("some string");
for (auto c : str) {
cout << c << endl;
}
//使用for循环改变字符串中的字符
string str("hello world");
for (auto& c : str) {
c = toupper(c);
cout << str << endl;
}
}
void testVector() {
//列表初始值还是元素数量
int val = 10;
vector<int>v1(10); //v1含有10个元素,每个元素的值为0
vector<int>v2{ 10 }; //v2含有一个元素,该元素的值为0
vector<string>v7{ 10 };//v7有10个默认初始化的元素
//vector<string>v6("hi"); 错误不可以使用字符串字面值构建vector对象
vector<int> v4(10, val); //包含了10个重复的元素每个元素的值都是val
//向vector容器中添加对象
vector<int>v2;
for (int i = 0; i != 100; ++i) {
v2.push_back(i);
}
//用for循环遍历vector
vector<int> v{ 1, 2, 3, 4 ,5, 6 };
for (auto& i : v) {
i *= i;
}
//vector的操作:v.empty() v.size()
//当元素值可以进行比较时,vector对象才能被比较
//vector不可以通过下标的方式添加元素,只能对已经存在的元素执行下标操作
vector<int> ivec;
for (decltype(ivec.size()) ix = 0; ix != 10; ix++) {
//ivec[ix] = ix; ivec是一个空数组,不包含任何元素,不可以通过下表的方式去访问他
}
for (decltype(ivec.size()) ix = 0; ix != 10; ++ix) {
ivec.push_back(ix);
}
//将string对象的第一个字母改为大写形式, *it表示迭代器所指元素的引用
string s("some string");
if (s.begin() != s.end()) {
auto it = s.begin();
*it = toupper(*it);
}
//迭代器类型
vector<int>::iterator it; //可以读写vector<int>中的元素
string::iterator it2;
vector<int>::const_iterator it3; //只可以读元素,不可以写元素
string::const_iterator it4;
vector<int>v;
const vector<int> cv;
auto it1 = v.begin(); //vector<int>::iterator
auto it2 = cv.begin();//vector<int>::const_iterator
//注意:但凡是使用了迭代器的循环体,都不要向迭代器所属的容器中添加元素,都会使该迭代器失效
//定义和初始化内置数组
unsigned cnt = 42;
constexpr unsigned sz = 42;
int* parr[sz]; //含有42个整形指针的数组
//字符数组的末尾含有空字符
char a1[] = { 'C', '++', '\0' };
//不允许对数组进行拷贝和赋值
/*
int a[] = {0, 1, 2};
int a2[] = a;
a2 = a;都是错误的行为
*/
//理解复杂的数组声明
int arr[10];
int* ptr[10];
int(*parry)[10] = &arr; //指向一个含有10个整数的数组
int(&arrref)[10] = arr;//引用一个含有10个整数的数组
int* (&arry)[10] = ptr; //arry是数组的引用该数组含有10 个指针
int ia[] = { 0, 1, 2, 3, 4 };
auto ia2(ia); //ia2是一个整形指针,指向ia的第一个元素
auto ia2(&ia[0]); //ia2的类型为int *
decltype(ia) ia3 = { 0, 1, 2, 3, 4 };
//指针也是迭代器
//标准库begin和end
int ia[] = { 0, 1, 2, 3, 4, 5, 6 };
int* beg = begin(ia);
int* last = end(ia);
auto n = beg - last; //arr中元素的数量
//c风格字符串的操作
//strlen(p) 获取p的长度 strcmp(p1,p2) 比较p1和p2的相等性 strcat(p1, p2) 将p2附加到p1后 strcpy(p1, p2) 将p2拷贝给p1
//混用string对象和c风格字符串
string s("hello world");
//不可以用string对象初始化char* char * str = s;
const char* str = s.c_str();
//使用范围for语句处理多维数组,除了最内层的循环外,其他所有的循环的控制变量都应该是引用类型
/*int ia[3][3] = { {1, 2, 3}, {3, 4, 5} };
size_t cnt = 0;
for (const auto& row : ia) {
for (auto col : row) {
//不理解为什么报错
}
}*/
//指针和多维数组
int ia[3][4];
int ib[3] = { 0, 1, 2 };
//int(*p)[4] = ia; //
//类型的别名简化多维数组的指针
using int_array = int[4];
typedef int int_array[4];
/*for (int_array* p = ia; p != ia + 3; ++p) {
for (int* q = *p; q != *p + 4; ++q) {
cout << *q << ' ';
}
cout << endl;
}
*/
}
int main() {
test();
return 0;
}