C++基础代码1

    科技2024-10-18  7

    STL

    C++ STL(标准模板库)是一套功能强大的 C++ 模板类。

    STL从广义上分为:容器(container)算法(algorithm)迭代器(iterator)

    STL的六大组件

    Container(容器) 各种基本数据结构,如vector、list、deque、set、map等,用来存放数据Algorithm(算法) 各种基本算法,如sort、find、copy、for_each…等Iterator(迭代器) 连接containers和algorithmsAdapter(适配器) 可改变containers、Iterators或Function object接口的一种组件Function object(仿函数) :行为类似函数、可作为算法的某种策略。Allocator(分配器) 负责空间的配置与管理。

    Vector

    容器:vector

    算法:for_each

    迭代器:vector<int>::interator

    #include <iostream> #include <vector> #include <algorithm> // 标准算法头文件 using namespace std; void myPrint(int val) { cout << val << endl; } void test01() { //创建一个vector容器 vector<int> v; //向容器中插入数据 v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); //通过迭代器访问容器中的数据 vector<int>::iterator itBegin = v.begin(); //起始迭代器 指向容器中的第一个元素 vector<int>::iterator itEnd = v.end(); //结束迭代器 指向容器中最后一个元素的下一个位置 //第一种遍历方式 while (itBegin != itEnd) { cout << *itBegin << endl; itBegin++; } //第二种遍历方式 for(vector<int>::iterator it = v.begin(); it != v.end(); it++){ cout << *it << endl; } //第三种遍历方式 利用STL提供的遍历算法 for_each(v.begin(), v.end(), myPrint); } int main() { test01(); return 0; }

     vector存放自定义数据类型

    #include <iostream> #include <vector> #include <string> using namespace std; class Person{ public: Person(string name, int age){ this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; void test01() { vector<Person> v; Person person1("zhangsan", 24); Person person2("lisi", 25); Person person3("wangerma", 26); //向容器中添加数据 v.push_back(person1); v.push_back(person2); v.push_back(person3); //遍历容器 for(vector<Person>::iterator it = v.begin(); it != v.end(); it++){ cout << "姓名:" << (*it).m_Name << "年龄:" << (*it).m_Age << endl; cout << "姓名:" << it->m_Name << "年龄:" << it->m_Age << endl; } } int main(){ test01(); return 0; } 姓名:zhangsan年龄:24 姓名:zhangsan年龄:24 姓名:lisi年龄:25 姓名:lisi年龄:25 姓名:wangerma年龄:26 姓名:wangerma年龄:26 #include <iostream> #include <vector> #include <string> using namespace std; class Person{ public: Person(string name, int age){ this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; void test01() { vector<Person *> v; Person person1("zhangsan", 24); Person person2("lisi", 25); Person person3("wangerma", 26); //向容器中添加数据 v.push_back(&person1); v.push_back(&person2); v.push_back(&person3); //遍历容器 for(vector<Person *>::iterator it = v.begin(); it != v.end(); it++){ cout << "姓名:" << (*it)->m_Name << "年龄:" << (*it)->m_Age << endl; } } int main(){ test01(); return 0; }

    string

    本质:

    string是C++风格的字符串,而string本质上是一个类

    string和char *区别:

    char *是一个指针string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器

    特点:

    string类内部封装了很多成员方法

    例如:查找find,拷贝copy,删除delete替换replace,插入insert

    sring管理char*所分配到额内存,不用担心复制越界和取值越界等,由类内部进行负责。

    string赋值操作

    功能描述:

    给string字符串进行赋值

    赋值的函数原型:

    string字符串拼接

     += append #include <iostream> #include <string> using namespace std; int main(){ string str1 = "I LOVE"; string str2 = "GAME"; str1 += str2; cout << "str1:" << str1 << endl; str1.append(str2); cout << "str1:" << str1 << endl; } str1:I LOVEGAME str1:I LOVEGAMEGAME

     string 字符串查找和替换

     find从左往右查找,rfind从右往左找

     replace

    #include <iostream> #include <string> using namespace std; //find void test01(){ string str1 = "I LOVE"; int pos = str1.find("LO"); if(pos == -1){ cout << "not find" << endl; } else{ cout << "pos:" << pos << endl; } } //replace void test02(){ string str1 = "abcdef"; //从1号位置起三个字符,替换成"1111 str1.replace(1, 3, "1111"); cout << "str1:" << str1 << endl; } int main(){ test01(); test02(); } pos:2 str1:a1111ef

    string字符存取

     char& operator[](int n)  //通过[]方式获取字符char& at(int n) #include <iostream> #include <string> using namespace std; void test01(){ string str1 = "abcdefg"; for(int i = 0;i < str1.size();i++){ cout << str1[i] << endl; } for(int i = 0;i < str1.size();i++){ cout << str1.at(i) << endl; } } int main(){ test01(); }

    string插入和删除

    insert 插入erase 删除 #include <iostream> #include <string> using namespace std; void test01(){ string str1 = "abcdefg"; //insert str1.insert(1, "haizei"); cout << str1 << endl; //erase str1.erase(1, 6); cout << str1 << endl; } int main(){ test01(); } ahaizeibcdefg abcdefg

    string子串

    substr #include <iostream> #include <string> using namespace std; void test01(){ string str1 = "abcdefg"; string sub_str = str1.substr(1,3); cout << sub_str << endl; } int main(){ test01(); }  bcd

     

     

     

    Processed: 0.019, SQL: 8