#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
void printvector(int val)
{
cout << val << " ";
}
void test1()
{
vector<int>v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
//每一个容器都有自己的迭代器, 迭代器用于遍历容器
//v.begin 返回一个迭代器 指向容器的第一个元素
//v.end()返回一个迭代器,指向容器的最后一个元素的下一个位置
//vector<int>::iterator pBegin = v.begin();可以这样写 根据c++11新特性 可以用下面的写法:
auto pBegin = v.begin();
auto pEnd = v.end();
//遍历
while (pBegin != pEnd)
{
cout << *pBegin << endl;
pBegin++;
}
printf("----------\n");
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
//for_each函数循环将容器中的元素传递给函数对象, 通过函数对象的operator()的具体实现来执行相应操作
for_each(v.begin(), v.end(), printvector);
}
//容器存放自定义的数据类型
class Person
{
//public可以被类外成员访问
//private可以被类成员访问和友元访问不能被类外成员访问
//protected可以被子类对象访问 不能在类外访问
public:
Person(string name, int age)
{
mName = name;
mAge = age;
}
public:
string mName;
int mAge;
};
//容器存放指针
void test2()
{
vector<Person>vp;
Person p1("aaaa", 20);
Person p2("bbbb", 30);
Person p3("cccc", 40);
Person p4("dddd", 50);
Person p5("eeee", 60);
Person p6("ffff", 70);
Person p7("aaaa", 20);
//添加元素
vp.push_back(p1);
vp.push_back(p2);
vp.push_back(p3);
vp.push_back(p4);
vp.push_back(p5);
vp.push_back(p6);
vp.push_back(p7);
//遍历
for (auto it = vp.begin(); it != vp.end(); it++)
{
cout << "name = " << (*it).mName << " age = " << (*it).mAge << endl;
}
}
//容器存放容器
void test3()
{
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
for (int i = 0; i < 4; i++)
{
v1.push_back(1 + i);
v1.push_back(2 + i);
v1.push_back(3 + i);
v1.push_back(4 + i);
}
vector<vector<int>>v;
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
//遍历
for (auto it = v.begin(); it != v.end(); it++)
{
for (auto vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *vit << " ";
}
cout << endl;
}
}
void test4()
{
string s1;//创建空字符串,调用无参构造函数
string s2(s1);//拷贝构造
const char *str = "hello";
string s4(str);
string s5(10, 'a');
cout << s4 << endl;
cout << s5 << endl;
}
//2 赋值操作
/*
string& operator=(const char* s);//char*类型字符串 赋值给当前的字符串
string& operator=(const string &s);//把字符串s赋给当前的字符串
string& operator=(char c);//字符赋值给当前的字符串
string& assign(const char *s);//把字符串s赋给当前的字符串
string& assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s);//把字符串s赋给当前字符串
string& assign(int n, char c);//用n个字符c赋给当前字符串
string& assign(const string &s, int start, int n);//将s从start开始n个字符赋值给字符串*/
void test5()
{
string s1;
string s2 = "aaaa";
s1 = "afddra";
s1 = s2;
s1 = 'a';
string s3;
s3.assign("binsbca");
string s4;
s4.assign(s3, 2, 3);
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
}
//3 存取操作
/*
char& operator[](int n);//通过[]方式取字符
char& at(int n);//通过at方法获取字符
*/
void test6()
{
string s = "hello world";
for (int i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
for (int i = 0; i < s.size(); i++)
{
cout << s.at(i) << " ";
}
cout << endl;
//[] 和 at 访问越界,[] 直接抱错, at 抛出异常out_of_range
try
{
//cout << s[100] << endl;
cout << s.at(100) << endl;
}
catch (...)
{
cout << "异常" << endl;
}
}
//4 拼接操作
/*
string& operator+=(const string& str);//重载+=操作符
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=()
string& append(const string &s, int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string& append(int n, char c);//在当前字符串结尾添加n个字符c
*/
void test7()
{
string s1 = "hello";
string s2 = "world";
s1 += s2;
cout << s1 << endl;
string s3;
s3 = s1 + s2;
cout << "s3 = " << s3 << endl;
string s4;
s4 += "ssss";
cout << "s4 = " << s4 << endl;
string s5 = "shenzhen";
s5.append(s1);
cout << "s5 = " << s5;
}
//5 查找和替换
/*
int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找
int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置
int find(const char c, int pos = 0) const; //查找字符c第一次出现位置
int rfind(const string& str, int pos = npos) const;//查找str最后一次位置,从pos开始查找
int rfind(const char* s, int pos = npos) const;//查找s最后一次出现位置,从pos开始查找
int rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置
int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s
*/
void test8()
{
string s1 = "shenzhen001";
int pos = s1.find("001", 2);
cout << pos << endl;
const char *str = "abin002";
s1.replace(3, 6, str);
cout << s1 << endl;
}
//6 比较
/*
compare函数在>时返回 1,<时返回 -1,==时返回 0。
比较区分大小写,比较时参考字典顺序,排越前面的越小。
大写的A比小写的a小。
int compare(const string &s) const;//与字符串s比较
int compare(const char *s) const;//与字符串s比较
*/
void test9()
{
string s1 = "hello";
string s2 = "hello";
const char *str = "aaa";
string s3 = "aaa";
int ret = s1.compare(s2);
cout << ret << endl;
ret = s1.compare(str);
cout << ret << endl;
ret = s3.compare(s1);
cout << ret << endl;
}
//7 子串
/*
string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串
*/
void test10()
{
string s = "usrname@abin.com";
int pos = s.find("@");
string name = s.substr(0, pos);
cout << name << endl;
}
//8 插入和删除
/*
string& insert(int pos, const char* s); //插入字符串
string& insert(int pos, const string& str); //插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符c
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符
*/
void test11()
{
string s1 = "hello";
const char *str = "123";
cout << s1.insert(2, str) << endl;
cout << s1.erase(2, 3) << endl;
}
//9 字符串转换
/*
//string 转 char*
string str = "itcast";
const char* cstr = str.c_str();
//char* 转 string
char* s = "itcast";
string sstr(s);
*/
void change(const char *a)
{
return;
}
void test12()
{
//c_string---->string
const char *str = "hell12o";
string s(str);
cout << s << endl;
//string---->c_string
string s1 = "world";
const char *str2 = s1.c_str();
cout << str2 << endl;
}
void test13()
{
string s = "asbcdsfsa";
char &a = s[2];
char &b = s[3];
a = '1';
b = '0';
cout << s << endl;
cout << (int *)s.c_str() << endl;
}
void test14()
{
string s = "a";
int *p = NULL;
int size = 0;
for (int i = 0; i < 100; i++)
{
s.append("a");
if ((int *)s.c_str() != p)
{
p = (int *)s.c_str();
cout << s.size() << endl;
cout << s.length() << endl;
}
}
}
//1 初始化
/*
vector<T> v; //采用模板实现类实现,默认构造函数
vector(v.begin(), v.end());//将v[begin(), end())区间中的元素拷贝给本身。
vector(n, elem);//构造函数将n个elem拷贝给本身。
vector(const vector &vec);//拷贝构造函数。
*/
void printv(vector<int>&v)
{
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
int arr[] = { 1,2,3,4,5 };
vector<int>v2(arr, arr + sizeof(arr) / sizeof(int));
vector<int>v3(10, 10);
vector<int>v4(v2);
printv(v2);
printv(v3);
printv(v4);
}
//2 赋值
/*
assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。
vector& operator=(const vector &vec);//重载等号操作符
swap(vec);// 将vec与本身的元素互换。
*/
void test02()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v1(arr, arr+sizeof(arr) / sizeof(int));
vector<int>v2;
v2.assign(v1.begin(), v1.end());
printv(v1);
printv(v2);
int arr1[] = {10,9,8,7,5 };
vector<int>v3(arr1, arr1 + sizeof(arr) / sizeof(int));
printv(v3);
cout << "交换后" << endl;
v1.swap(v3);
printv(v1);
printv(v3);
}
//3 大小操作
/*
size();//返回容器中元素的个数
empty();//判断容器是否为空
resize(int num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem);//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长>度的元素被删除。
capacity();//容器的容量
reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。
*/
void test03()
{
int arr[] = { 1,2,3,4,5 };
vector<int>v1(arr, arr + sizeof(arr) / sizeof(int));
if (v1.empty())
{
cout << "空" << endl;
}
else
{
cout << "size : " << v1.size() << endl;
}
printv(v1);
v1.resize(10);
printv(v1);
v1.resize(20, 1);
printv(v1);
cout <<"size :"<< v1.size() << endl;
cout << "capacity():" << v1.capacity() << endl;
}
int main()
{
//test1();
//test2();
//test3();
printf("---下面是string---:\n");
//test4();
//test5();
//test6();
//test7();
//test8();
//test9();
//test10();
//test11();
//test12();
//test13();
//test14();
printf("---vertor:\n");
//test01();
//test02();
test03();
return 0;
}
待续