1.Vector尾部添加操作
eg.
include<vector>
using namespace std;
vector<int> vec={1,2,3,4};
vec.push_back(5); //在尾部进行元素插入操作
eg2.
// 创建动态数组vector
vector<int> vec = { 1,2,3,4 };
cout << "size is " << vec.size() << endl;//size is 4
cout << "capacity is " << vec.capacity() << endl;//capacity is 4
// 在尾部插入一个元素5
vec.push_back(5);
cout << "size is " << vec.size() << endl;//size is 5
cout << "capacity is " << vec.capacity() << endl;//capacity is 6
2.Vector尾部删除操作
(1) vec.pop_back();
(2) vec.erase(vec.end()-1)
3.Vector中间插入操作
// 在中间插入一个元素6
vec.insert(--vec.end(), 6);//1,2,3,4,5
cout << "size is " << vec.size() << endl;
cout << "capacity is " << vec.capacity() << endl;
// 遍历所有元素
for (int index = 0; index < vec.size(); ++index)
{
cout << vec[index] << endl;//1,2,3,4,6,5
}
字符串
// 注意考虑最后以空字符(‘\0’)结束,所以数组个数比正常的+1
char strHelloWorld[11] = { "helloworld" }; // 这个定义可以
//指针表示方式
char* pStrHelloWrold = "helloworld";
pStrHelloWrold = strHelloWorld;
// 计算字符串长度
cout << "字符串长度为: " << strlen(strHelloWorld) << endl;
cout << "字符串占用空间为: " << sizeof(strHelloWorld) << endl;
char[]和char*的 区别,把握两点:
(1)地址和地址存储的信息
(2)可变与不可变
常见ASCII码
‘A’:0x41
'a':0x61
'0':0x30
DEL:0x7F
字符串常见操作
// 字符串定义
string s1;//定义空字符串
string s2 = "helloworld";//定义并初始化
string s3("helloworld");
string s4 = string("helloworld");
// 获取字符串长度
cout << s2.length() << endl;//10
cout << s2.size() << endl;//10
cout <<sizeof(s2)<<endl;//11
cout << s2.capacity() << endl;//15
// 字符串比较strcmp(s1,s2)
s1 = "hello", s2 = "world";
cout << (s1 == s2) << endl;
cout << (s1 != s2) << endl;
// 转换成C风格的字符串
const char *c_str1 = s1.c_str();
cout << "The C-style string c_str1 is: " << c_str1 << endl;
// 随机访问
for (unsigned int index = 0; index < s1.length(); ++index)
{
cout << c_str1[index] << " ";
}
cout << endl;
for (unsigned int index = 0; index < s1.length(); ++index)
{
cout << s1[index] << " ";
}
cout << endl;
// 字符串拷贝
s1 = "helloworld";
s2 = s1;//或者strcpy(s2,s1)
// 字符串连接
s1 = "helllo", s2 = "world";
s3 = s1 + s2; //s3: helloworld
s1 += s2; //s1: helloworld
指针相关
指针的数组 Tt[]
数组的指针 T(t)[]
eg.
// array of pointers和a pointer to an array
int c[4] = { 0x80000000, 0xFFFFFFFF, 0x00000000, 0x7FFFFFFF };
int* a[4]; // array of pointers 指针的数组
int(*b)[4]; // a pointer to an array 数组的指针
b = &c; // 注意:这里数组个数得匹配
// 将数组c中元素赋给数组a
for (unsigned int i = 0; i<4; i++)
{
a[i] = &(c[i]);
}
// 输出看下结果
cout << (a[0]) << endl; // -2147483648
cout << (b)[3] << endl; // 2147483647
const与指针
eg.
char strHelloworld[] = { "helloworld" };
char const* pStr1 = "helloworld"; // const char*
char* const pStr2 = strHelloworld;
char const* const pStr3 = "helloworld"; // const char* const
pStr1 = strHelloworld;
//pStr2 = strHelloworld; // pStr2不可改
//pStr3 = strHelloworld; // pStr3不可改
unsigned int len = strnlen_s(pStr2, MAX_LEN);
cout << len << endl;
for (unsigned int index = 0; index < len; ++index)
{
//pStr1[index] += 1; // pStr1里的值不可改
pStr2[index] += 1;
//pStr3[index] += 1; // pStr3里的值不可改
}
指针操作
eg.
char ch = 'a';
char* cp = &ch;
// ++,--操作符
char* cp2 = ++cp;
char* cp3 = cp++;
char* cp4 = --cp;
char* cp5 = cp--;
// ++ 左值
//++cp2 = 97;
//cp2++ = 97;
// *++, ++*
*++cp2 = 98;
char ch3 = *++cp2;
*cp2++ = 98;
char ch4 = *cp2++;
// ++++, ----操作符等
int a = 1, b = 2, c, d;
//c = a++b; // error
c = a++ + b;
//d = a++++b; // error
char ch5 = ++*++cp;