// 面向对象 3-14 下午.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//#include
//int main()
//{
// std::cout << “Hello World!\n”;
//}
//
运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
调试程序: F5 或调试 >“开始调试”菜单
//
入门使用技巧:
1. 使用解决方案资源管理器窗口添加/管理文件
2. 使用团队资源管理器窗口连接到源代码管理
3. 使用输出窗口查看生成输出和其他消息
4. 使用错误列表窗口查看错误
5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
//
//#include
//using namespace std;
类中也是可以使用模板进行操作的。
首先你得确定的就是模板的作用,就是类似于一个磨具,提高代码的复用,还有就是提高代码的可读性。
//template
//class seqList
//{
//public:
使用构造函数 进行初始化。在初始化的列表中进行初始化。
// seqList(int n) //参数的列表
// :_data(new T(n))
// , _size(0)
// , _capacity(n)
// {}
模板的位置。
//T seqList(size_t pos);
//private:
// //int* _data;
// T* _data;
// size_t _size;
// size_t _capacity;
//};
类外定义函数;需要加泛型声明 才能进行模的板使用。
//template
//T seqList::seqListAt(size_t pos)
//{
// return _data[pos];
//}
//void test()
//{
错误写法
// //seqList sq(10);
// //类模板实例化之后,真正的类型–>类名<模板参数类型>
// seqList sq(10);
// seqList sq2(10);
//}
//int main()
//{
// test();
// return 0;
//}
标准模板库 stl
string
//#include
//using namespace std;
注意的就得是实现的规则。
//void test()
//{
// //default (1)
// // string();
// string str;
//
// //copy(2)
// // string(const string& str);
// string copy(str);//拷贝构造
//
// string str2(“abc”);
// /substring(3)
// string(const string& str, size_t pos, size_t len = npos);/
// //部分字符串的创建
// string substr(str2, 1, 2);
// /from c - string(4)/
// /string(const char s);/
// string str3(“12345678”, 5);//12345
// //string(const char s, size_t n);
// string str4(10, ‘a’);//aaaaaaaaaa;
// string str5 = “abcde”;
// //赋值的操作
// str5 = str4;//这里是通过对象来进行赋值。
// str5 = “123”;//通过字符串进行赋值。
// str5 = ‘b’;//通过字符来进行赋值。
//
//
//}
//int main()
//{
// test();
// return 0;
//}
//
string 接口
//#include
//using namespace std;
//void test()
//{
// const string str = “12345”;
// string str2 = “12345”;
// char ch = str[3];
// const char& ref = str[3];
// //const对象,调用接口 const char& operator[](size_t pos) const
// /ref = ‘c’;
// str[3] = ‘a’;/
//
// //非const对象,调用接口;char& operator[](size_t pos)
// char& ref2 = str2[3];
// str2[3] = ‘a’;
str2[10] = ‘b’;//出现断言的操作
// str2.at(3) = ‘b’;
str2.at(10) = ‘b’; 使用at如果出现异常那就会抛异常。
//
// //相比之下[]可读性较高。相比于at访问的情况下。
//}
//int main()
//{
// test();
// return 0;
//}
//迭代器
//迭代器;设计模式–>元素访问的一种设计模式
//所有容器都需要遵循相同的设计规范
//所有容器迭代器的使用方式都是相同的
//怎么使用
//迭代器的指针方式和指针类似
//设计规范
//1.begin ;指向第一个元素的位置
//2.end迭代器;指向最后一个元素的位置
//3.访问数据;通过解引用完成
//4.迭代器移动;++移动到下一个元素的位置,–移动到上一个元素的位置
//5.位置的判断;支持!=,==
//有些容器具有反向迭代器
//6.rbegin迭代器;指向最后一个元素的最后一个位置
//7.rend迭代器;指向第一个元素的前一个位置。
迭代器的使用
//#include
//#include
//using namespace std;
//void test()
//{
// //正向迭代器
// string str = “12345”;
// //获取起始位置的迭代器
// string::iterator it = str.begin();
// for (; it != str.end(); ++it)
// {
// //迭代器的解引用
// cout << *it << " ";
// //对迭代器中内容的修改
// *it = ‘a’;
// cout << *it << endl;
// }
//
// //只读迭代器
// const string str2 = “abcde”;
// //只能用const来接收
// string::const_iterator it2 = str2.begin();
// while (it2 != str2.end())
// {
// cout << *it2 << " ";
// //只读迭代器不能修改内容
// //*it2 = ‘1’; 这里是不能进行修改的。
// ++it2;
// }
//}
//int main()
//{
// test();
// return 0;
//}
反向迭代器
//#include
//#include
//using namespace std;
//void test()
//{
// //反向迭代器
// string str = “12345”;
// //最后一个元素的位置
// string::reverse_iterator it = str.rbegin();
// while (it!=str.rend)
// {
// cout << *it << " ";//54321
// //反向移动
// //也是可以进行修改的。
// *it = ‘a’;
// ++it;
// //正向是向后移动,反向就是向前移动<----
// }
//
// //只读反向迭代器
// const string str2 = “abcde”;
// string::const_reverse_iterator it2 = str2.rbegin();
// while (it2 != str2.rend())
// {
// cout << *it2 << " ";
// //*it2 = ‘1’;这里也是不能对常量进行赋值的。
// ++it2;
// }
// string::const_reverse_iterator cit = str.crbegin();
// while (cit != str.crend)
// {
// cout << *cit << " ";
// //*cit = ‘a’; //这里表示的和const是一致的。不能进行修改。
// ++cit;
// }
//}
//int main()
//{
// test();
// return 0;
//}
//
//
//#include
//#include
//using namespace std;
范围for的操作
//void test()
//{
// //范围for的操作 可以遍历访问范围确定的情况。
// int arr[] = { 1,2,3,4,5 };
// for (const auto& e : arr)
// {
// cout << e << " ";
// }
// cout << endl;
// //这里使用范围for来进行数值的改变。
// for (auto& e : arr)
// {
// e = 10;
// }
// string str = “12345”;
// //范围for;实际上是通过迭代器实现的
// //支持迭代器访问的自定义类型都可以支持范围for
//}
//int main()
//{
// test();
// return 0;
//
//}
//#include
//#include
//using namespace std;
基本的for循环
//void test()
//{
// string str = “12345”;
// for (size_t i = 0; i < str.size(); ++i)
// {
// cout << str[i] << " ";
// str[i] = ‘a’;
// }
// cout << endl;
//}
//
//int main()
//{
// test();
// return 0;
//
//}
string 的访问方式
1.for循环+operator[]
2.迭代器
3.范围for
这三种方式都可以修改内容
//#include
//#include
//using namespace std;
容量相关的
//void test()
//{
// //resize;修改有效字符的个数
// //resize(n);如果有效字符的个数增加,则新增的位置填充‘\0’
// //resize(n,ch);如果有效字符的个数增加,则新增的位置填充ch
// //如果size减小,不会进行填充。
// string str;
// cout << str.size() << endl;
// //resize;修改有效字符的个数
// str.resize(10);
// cout << str.size() << endl;
// string str2 = “123”;
// cout << str2.size() << endl;
// str2.resize(10);
// cout << str2.size() << endl;
// str2.resize(2);
// cout << str2.size() << endl;
// str2.resize(5, ‘a’);//
// cout << str2 << endl;
//}
//int main()
//{
// test();
// return 0;
//
//}
//#include
//#include
//using namespace std;
capacity表示的是当前现有可以存放最多有效元素的个数。
//void test()
//{
// string str = “123”;
// int sz = str.size();
// cout << str << endl;
// //capacity;当前string中最多可以存放的元素个数
// int cap = str.capacity();
// cout << cap << endl;
// str.resize(10);
// sz = str.size();
// cap = str.capacity();
// str.resize(1);
// sz = str.size();
// cap = str.capacity();
// //resize; 当调整之后的size大于容量时,容量也会发生变化
// str.resize(20);
// sz = str.size();
// cap = str.capacity();
//
// //reserve;修改容量 就是修改的是capacity大小。 只能增加容量,不影响size大小。不能减小容量。
// str.reserve(60);
// sz = str.size();
// cap = str.capacity();
// str.reserve(10);
// sz = str.size();
// cap = str.capacity();
// //clear;清空有效字符,但是不会影响容量,size会发生变化
// str.clear();
// sz = str.size();
// cap = str.capacity();
//}
//int main()
//{
// test();
// return 0;
//}
//
//
//
//#include
//#include
//using namespace std;
shrink_to_fit 这其实是一种空间的优化
//void test()
//{
// string str = “123”;
// int sz = str.size();
// int cap = str.capacity();
// str.shrink_to_fit();
// sz = str.size();;
// cap = str.capacity();
//
//
// str.reserve(100);
// str.shrink_to_fit();
// sz = str.size();
// cap = str.capacity();
//
//
//
// str.reserve(1000);
// str.shrink_to_fit();
// sz = str.size();
// cap = str.capacity();
//
//
// str.reserve(10000);
// str.shrink_to_fit();
// sz = str.size();
// cap = str.capacity();
//}
//int main()
//{
// test();
// return 0;
//}
//#include
//#include
//using namespace std;
空间增容的操作
//void test()
//{
// string str;
//
// //提前开好空间,避免频繁的增容,提高代码的效率
// str.reserve(200);
// int cap = str.capacity();
// cout << cap << endl;
// for (int i = 0; i < 200; ++i)
// {
// str.push_back(‘a’);
// if (cap != str.capacity())
// {
// cap = str.capacity();
// cout << cap << endl;
// }
// }
//}
//int main()
//{
// test();
// return 0;
//}
#include
#include
using namespace std;
//修改相关的函数
//operator+=
void test()
{
string str;
string str2 = “123”;
cout << str2 << endl;
str += str2;
str += “abc”;
cout << str << endl;
str += ‘4’;
str.operator+=(‘5’);
cout << str << endl;
}
int main()
{
test();
return 0;
}
//使用迭代器
//
//string& append(InputIterator first, InputIterator last);
//append接口
void test()
{
string str;
string str2 = “123”;
str.append(str2);//123
str.append(str2, 1, 1);//1232
str.append(“abc”);//1232abc
str.append(“123456”, 6);//1232abc123456
str.append(5, ‘m’);//1232abc123456mmmmm
char arr[] = “abcdefg”;
str.append(arr, arr + sizeof(arr) / sizeof(arr[0]));//1232abc123456mmmmmabcdefg
str.append(str2.begin(), str2.end());//1232abc123456mmmmmabcdefg\0123
}
//赋值的运算符。类似于=的赋值的操作
void test()
{
string str;
string str2 = “123”;
str.assign(str2);//123
str.assign(str2, 1, 1);//2
str.assign(“abc”);//abc
str.assign(“abcde”, 2);//ab
str.assign(5, ‘a’);//aaaaa
str.assign(++str2.begin(), str2.end());//23
}
//插入的接口 注意的是接口的规则。
void test()
{
string str;
string str2 = “123”;
str.insert(0, str2);//abc123
str.insert(4, str2, 1, 2);//abc1bc23
str.insert(str.size(), “abc”);//abc1bc23abc
str.insert(5, “12345”, 4);//abc1b 1234 c23abc
str.insert(str.begin(), 2, ‘0’);//00 abc1b1234c23abc
//泛型迭代器
str.insert(str.end(), str2.begin(), str2.end());//00 abc1b1234c23abc123
}