当前位置: 首页 > news >正文

创建一个网站的步骤网页搜索框下记录删不掉

创建一个网站的步骤,网页搜索框下记录删不掉,网站小视频怎么做的,做网站业务员文章目录 前言一、string 是什么#xff1f;二、C语法补充1. auto2. 范围for 三、string类对象的常见构造1. Construct string object2. String destructor3. operator 四、string迭代器相关1. begin与end1#xff09;begin2#xff09;end3#xff09;使用 2. rbegin 与 r… 文章目录 前言一、string 是什么二、C语法补充1. auto2. 范围for 三、string类对象的常见构造1. Construct string object2. String destructor3. operator 四、string迭代器相关1. begin与end1begin2end3使用 2. rbegin 与 rend1rbegin2rend3使用 五、容量相关1. resize2. reserve3. 使用 六、元素的访问相关1. operator[]2. at3. back4. front 七、 string类对象的修改操作1. append2. operator3. assign4. insert5. erase6. replace八、对于字符串操作部分1. c_str()2. find 与 rfind3. substr 九、非成员函数函数重载1. operator2. operator 与 orerator 3. getline 总结 前言 string接口总结 分类函数名功能示例代码构造函数std::string()默认构造空字符串std::string s1;std::string(const char* s)从 C 字符串构造std::string s2(Hello);std::string(const std::string str)拷贝构造函数std::string s3(s2);std::string(const char* s, size_t n)使用部分 C 字符串构造std::string s4(Hello, 3);std::string(size_t n, char c)使用 n 个字符 c 构造std::string s5(5, x);std::string(InputIt first, InputIt last)通过迭代器范围构造std::string s6(v.begin(), v.end());析构函数~std::string()销毁字符串对象无需直接调用赋值操作符operator赋值操作std::string s1 s2;operator(const char* s)通过 C 字符串赋值s1 Hello;operator(char c)通过单个字符赋值s1 A;operator(std::initializer_listchar)通过初始化列表赋值s1 {H, i};迭代器begin()返回指向开头的迭代器auto it s.begin();end()返回指向末尾后的迭代器auto it s.end();rbegin()返回指向反向开始的迭代器auto rit s.rbegin();rend()返回指向反向末尾后的迭代器auto rit s.rend();cbegin()返回指向常量开头的迭代器auto cit s.cbegin();cend()返回指向常量末尾后的迭代器auto cit s.cend();crbegin()返回指向常量反向开始的迭代器auto crit s.crbegin();crend()返回指向常量反向末尾后的迭代器auto crit s.crend();容量管理size()返回字符串长度size_t len s.size();length()返回字符串长度 (同 size())size_t len s.length();max_size()返回字符串可容纳的最大大小size_t maxLen s.max_size();resize(size_t n)调整字符串大小s.resize(10);resize(size_t n, char c)调整大小并用字符填充s.resize(10, x);capacity()返回分配的存储容量size_t cap s.capacity();reserve(size_t n)保留指定容量s.reserve(20);clear()清空字符串s.clear();empty()判断字符串是否为空bool isEmpty s.empty();shrink_to_fit()释放未使用的容量s.shrink_to_fit();元素访问operator[] (size_t pos)访问指定位置的字符char c s[2];at(size_t pos)安全访问指定位置的字符char c s.at(2);back()访问最后一个字符char c s.back();front()访问第一个字符char c s.front();修饰符operator (const std::string str)追加字符串s s2;operator (const char* s)追加 C 字符串s Hello;operator (char c)追加单个字符s A;append(const std::string str)追加字符串s.append(s2);append(const std::string str, size_t subpos, size_t sublen)追加子字符串s.append(s2, 1, 3);append(const char* s)追加 C 字符串s.append(Hello);append(const char* s, size_t n)追加 C 字符串的前 n 个字符s.append(Hello, 3);append(size_t n, char c)追加 n 个字符s.append(5, x);assign(const std::string str)分配字符串s.assign(s2);assign(const char* s)分配 C 字符串s.assign(Hello);insert(size_t pos, const std::string str)在指定位置插入字符串s.insert(3, ABC);erase(size_t pos 0, size_t len npos)删除指定位置的字符s.erase(2, 4);replace(size_t pos, size_t len, const std::string str)替换部分字符串s.replace(2, 3, XYZ);swap(std::string str)交换两个字符串的内容s.swap(s2);pop_back()删除最后一个字符s.pop_back();字符串操作c_str()返回 C 字符串指针const char* cstr s.c_str();data()返回指向字符串内容的指针const char* data s.data();copy(char* s, size_t len, size_t pos 0)将字符串复制到字符数组中s.copy(arr, 5);find(const std::string str, size_t pos 0)查找字符串中首次出现的子串size_t pos s.find(Hello);rfind(const std::string str, size_t pos npos)查找字符串中最后一次出现的子串size_t pos s.rfind(Hello);find_first_of(const std::string str, size_t pos 0)查找字符集中的任意一个字符size_t pos s.find_first_of(aeiou);find_last_of(const std::string str, size_t pos npos)查找字符集中最后出现的字符size_t pos s.find_last_of(aeiou);find_first_not_of(const std::string str, size_t pos 0)查找第一个不属于字符集的字符size_t pos s.find_first_not_of(aeiou);find_last_not_of(const std::string str, size_t pos npos)查找最后一个不属于字符集的字符size_t pos s.find_last_not_of(aeiou); || | substr(size_t pos 0, size_t len npos) | 生成子字符串 | std::string sub s.substr(0, 5); | | | compare(const std::string str) | 比较两个字符串 | int result s.compare(s2); | | 成员常量 | npos | 表示 size_t 的最大值 | if (s.find(abc) std::string::npos) {} | | 非成员函数 | operator | 字符串连接 | std::string s3 s1 s2; | | | operator | 插入字符串到输出流 | std::cout s; | | | operator | 从输入流中提取字符串 | std::cin s; | | | getline() | 从输入流中读取一行到字符串 | std::getline(std::cin, s); | | | swap(std::string a, std::string b) | 交换两个字符串 | std::swap(s1, s2); | 一、string 是什么 这是一个模板类 它用来管理C中字符类型的数据字符除了英文数字还有各国的语言等等他们有不同字节的大小 平常我们在使用的时候用的最多的就是 以char为数据类型实例化模版basic_string为类类型basic_string再将basic_stringtypedef为string 二、C语法补充 1. auto 早期C/C中的auto 在C语言中auto用于声明具有自动存储期的局部变量表示变量在函数调用时自动分配并在函数返回时释放。因为局部变量默认具有自动存储期所以auto修饰符不太被使用逐渐变得不重要。 C11中的auto新含义 C11标准重新定义了auto使其成为一种类型推导机制。编译器根据初始化的表达式自动推导出变量的类型。例如 auto x 10; // x 的类型是 int auto y 3.14; // y 的类型是 double指针和引用 当使用auto声明指针时auto和auto*效果相同编译器自动推导出指针类型int* ptr x; auto p ptr; // p 的类型是 int*如果要声明引用必须明确加上int a 5; auto ref a; // ref 是 a 的引用多个变量声明 如果在一行中声明多个变量所有变量必须是相同的类型。因为编译器只推导第一个变量的类型 auto x 1, y 2.5; // 错误x 是 inty 是 double改为 auto x 1; auto y 2.5; // 正确函数返回值 auto不能用于函数参数类型但可以用于推导函数返回值类型。不过要注意这样用可能不直观 auto func() {return 42; // 返回类型为 int }数组声明 不能直接用auto声明数组因为数组的大小是编译时必须明确的编译器无法推导出数组的大小 auto arr[] {1, 2, 3}; // 错误2. 范围for // 范围for C11 语法糖 // // 适用于容器遍历和数组遍历 // // 自动取容器的数据赋值给左边的对象(仅仅是拷贝了一份也就是赋值) // // 自动自动判断结束 // // 原理范围for底层是迭代器基于范围的for循环概述 C11引入了基于范围的for循环主要是为了减少程序员在遍历时的工作量避免手动设置迭代器或索引从而简化代码并降低出错的风险。语法为 for (元素类型 变量名 : 容器) {// 对变量名进行操作 }第一部分元素类型 变量名用来保存遍历过程中当前元素的拷贝或引用。第二部分容器是要遍历的数组或容器。它会自动 迭代元素提取数据判断何时结束。 适用范围 范围for可以遍历数组、容器对象如std::vector、std::list等并且也能遍历自定义的类只要这些类实现了迭代器接口。 示例代码 遍历数组 int arr[] {1, 2, 3, 4, 5}; for (int num : arr) {std::cout num std::endl; }遍历容器如std::vector std::vectorint vec {10, 20, 30, 40}; for (int val : vec) {std::cout val std::endl; }通过引用遍历并修改元素 如果你希望在遍历时修改容器中的元素可以使用引用 for (int val : vec) {val * 2; // 将每个元素都乘以2 }底层机制 范围for的底层其实会被转换成传统的迭代器形式相当于以下代码 for (auto it container.begin(); it ! container.end(); it) {auto val *it; // 取出元素// 循环体 }它通过容器的begin()和end()函数获取迭代器自动完成遍历操作。从汇编层面来看它生成的指令与使用迭代器的常规for循环类似只是更加简洁。 三、string类对象的常见构造 1. Construct string object 语法作用string();空的默认构造string (const string str);拷贝构造string (const string str, size_t pos, size_t len npos);从str第pos个位置拷贝len个长度超了就拷贝到结尾string (const char* s);拷贝一个字符串string (const char* s, size_t n);拷贝字符串前n个值string (size_t n, char c);拷贝n个’c’字符string (InputIterator first, InputIterator last);迭代器左闭右开指定迭代器区间 2. String destructor 就是string类的析构函数 3. operator 语法作用string operator (const string str);可以赋值一个stringstring operator (const char* s);可以赋值一个数组string operator (char c);可以赋值一个’c’可以两个对象之间加减也可以 string或其他字符串 四、string迭代器相关 1. begin与end 1begin 提供了两个版本对于普通变量和const变量 返回字符串第一个有效位置的字符的迭代器 2end 提供了两个版本对于普通变量和const变量 返回字符串最后一个有效位置的字符的下一个位置的迭代器string的末尾会加’\0’而’\0’不算有效字符因此通常end在’\0’的位置。 如果是空字符返回与begin一样的值。 3使用 int main() {std::string st;st hellow world;auto it st.begin();while (it ! st.end()){cout *it;it;}cout endl; } int main() {std::listint lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);//std::listint::iterator it lt.begin();auto it lt.begin();while (it ! lt.end()){cout *it;it;}cout endl; }2. rbegin 与 rend 用于逆转迭代 1rbegin 返回字符串最后一个有效数据的迭代器 2rend 返回第一个有效数据的前一个理论元素的迭代器(被认为是字符串的反向结束)。 3使用 int main() {std::string st hellow world;std::string::reverse_iterator it st.rbegin();while (it ! st.rend()){cout *it;//相当于反向了it;}cout endl;return 0; } 五、容量相关 名字作用size / length返回字符串有效长度max_size返回字符产可以到达的最大长度取决于编译器resize使字符串变为n个字符若变小了就取前n个元素销毁后面的。若变大了在后面插入若指定了一个字符就插入该字符否则初始化为空字符 ———————————————————void resize (size_t n); void resize (size_t n, char c);capacity返回空间总大小vs在这里对于小空间有一个数组buffer[16]空间大了就用别的1.5倍率扩容reserve为字符串预留空间void reserve (size_t n 0);clear清空字符串有效字符不回收空间empty返回bool字符串是否为空shrink_to_fit是一个非强制的方法它向容器的分配器发出内存收缩的请求但不一定保证能立即释放多余的内存 1. resize 使字符串变为n个字符若变小了就取前n个元素销毁后面的。若变大了在后面插入若指定了一个字符就插入该字符否则初始化为空(‘\0’)字符 int main() {string s1(11111111111111111111);cout s1 endl;cout s1.size() endl;cout s1.capacity() endl;// 删除// n sizes1.resize(15);cout s1 endl;cout s1.size() endl;cout s1.capacity() endl;// 插入// size n capacitys1.resize(25, x);cout s1 endl;cout s1.size() endl;cout s1.capacity() endl;// n capacitys1.resize(40, x);cout s1 endl;cout s1.size() endl;cout s1.capacity() endl;return 0; }2. reserve int main() {string s1;// 提前开空间避免扩容s1.reserve(200);return 0; }3. 使用 int main() {try{string s1(hello world11111);cout s1.size() endl;cout s1.length() endl;cout s1.max_size() endl;cout s1.capacity() endl;cout endl endl;s1.clear();cout s1.size() endl;cout s1.capacity() endl;//s1[20];s1.at(20);}catch (const exception e){cout e.what() endl;}return 0; } 六、元素的访问相关 1. operator[] 返回字符串对应位置的字符有了这个我们就可以把string的对象当数组用 2. at 和[ ]的作用一样不过失败越界访问了要抛出一个异常这样接收 抛出来的是这样一个异常我们用他的父类接收 int main() {try{string s1(hello world11111);s1.at(20);}catch (const exception e){cout e.what() endl;}return 0; }3. back 通过它访问字符串最后一个有效字符 4. front 通过它访问字符串第一个字符 七、 string类对象的修改操作 成员函数功能描述operator将指定的字符串或字符追加到现有字符串末尾。支持 std::string 对象和 C 风格字符串const char*的追加操作。append向字符串末尾追加内容支持多种形式完整字符串、子串、C 风格字符串、字符数组的一部分、重复某个字符等。push_back向字符串末尾追加一个单个字符简洁方便。assign替换整个字符串的内容可以使用另一个字符串、子串、C 风格字符串、字符数组或重复某个字符的方式进行赋值。insert在指定位置插入内容支持插入字符、子串、C 风格字符串或重复某个字符。erase从字符串中删除指定位置的字符或字符范围。可以删除单个字符或一段子串。replace用指定的内容替换字符串中的某个范围可以替换子串、C 风格字符串或重复某个字符的部分。swap交换两个字符串的内容不涉及内存拷贝因此比赋值操作更高效。pop_back删除字符串的最后一个字符减少字符串的长度。 1. append 它的作用是在字符串后加入一个字符串。 字符串 语法: append(const string str)例子:string s Hello; s.append( World); // s Hello World字符串的子串 语法: append(const string str, size_t subpos, size_t sublen)例子:string s Hello; s.append(World!, 0, 3); // 追加World!的前3个字符 // s Hello WorC风格字符串 语法: append(const char* s)例子:string s Hello; s.append( World); // s Hello World字符数组的一部分 语法: append(const char* s, size_t n)例子:string s Hello; s.append(World!!!, 5); // 只追加前5个字符 // s Hello World重复指定字符 语法: append(size_t n, char c)例子:string s Hello; s.append(3, !); // s Hello!!!迭代器范围 语法: append(InputIterator first, InputIterator last)例子:string s Hello; string part World; s.append(part.begin(), part.begin() 3); // 追加World的前3个字符 // s Hello Wor2. operator 可以把字符串和字符加到当前字符串后面 string s3(hello);s3 ,;s3 world;cout s3 endl;3. assign 替换原本的字符串一功能替换成一下这几种方式 4. insert 任意位置前插入有以下这些插入方法。 5. erase 什么都不传就全删完了 6. replace 相当于assign的升级版可以只替换一部分。平替的时候效率很高一旦个数不同就牵扯到遍历字符串 八、对于字符串操作部分 函数名功能c_str()获取 C 风格字符串的等价物以 null 结尾的字符数组。data()获取字符串的数据返回指向字符的指针。get_allocator()获取分配器返回用于分配存储的 allocator 对象。copy(char* s, size_t len, size_t pos 0)从字符串中复制字符序列到指定缓冲区。find(const string str, size_t pos 0)在字符串中查找子字符串的首次出现。rfind(const string str, size_t pos npos)在字符串中查找子字符串的最后一次出现。find_first_of(const string str, size_t pos 0)查找字符串中第一个出现的指定字符。find_last_of(const string str, size_t pos npos)从末尾开始查找字符串中最后一个出现的指定字符。find_first_not_of(const string str, size_t pos 0)查找字符串中第一个不匹配的指定字符。find_last_not_of(const string str, size_t pos npos)从末尾开始查找字符串中最后一个不匹配的指定字符。substr(size_t pos 0, size_t len npos)生成指定位置和长度的子字符串。compare(const string str)比较两个字符串的内容返回整数结果。 1. c_str() 就是把C的string转换为C语言的const char* 假设我要用c的接口操作文件 string s1(hello world); cout s1 endl; cout s1.c_str() endl;string s2(Test.cpp); FILE* fout fopen(s2.c_str(), r); char ch fgetc(fout); while (ch ! EOF) {cout ch;ch fgetc(fout); }2. find 与 rfind size_t find(const string str, size_t pos 0) const 功能: 在当前字符串中查找子字符串 str 的首次出现位置。参数: const string str: 要查找的子字符串。size_t pos: 开始查找的位置默认为 0。 返回值: 如果找到返回子字符串的起始索引如果未找到返回 std::string::npos。 示例: std::string str Hello, world!; size_t pos str.find(world); // 返回 7rfind就是倒着找 find_first_of 是比如给了 ” aeiou“就找第一个出现任意一个这里面字符的下标 find_last_of 就是 find_first_of 倒着找 find_first_not_of 是比如给了 ” aeiou“就找第一个不出现一个这里面字符的下标 find_last_not_of 就是 find_first_not_of 倒着找 3. substr 从第pos个位置获取 len 个字串不指定len就从pos位置开始全部获取 九、非成员函数函数重载 1. operator 做加法就好 2. operator 与 orerator 3. getline 用cin输入的时候默认空格和回车会结束到下一个。 getline默认回车到下一个也可以自定义结束符。 int main() {string str;//cin str;//getline(cin, str, #);getline(cin, str);return 0; }总结 到这里string用法的讲解就结束啦~ 谢谢大家~
http://www.w-s-a.com/news/729767/

相关文章:

  • 网站建好以后每年都续费么wordpress 仿聚划算
  • 单位网站建设收费标准网上开店铺需要多少钱
  • 灯饰网站需要这么做申请域名的流程
  • 软件下载网站怎么赚钱wordpress减少数据库查询
  • 什么兼职网站可以做视频剪辑常见的推广平台有哪些
  • 网站开发是用html还是jsp设迹官网
  • 查公司信息的网站怎么学wordpress
  • 白银做网站长春一般建一个网站需要多少钱
  • 帮人做钓鱼网站的人网络推广培训职业学校
  • 淘宝客有必须做网站吗网站开发的形式有( )
  • 网站建设:上海珍岛网页版qq空间登录
  • 网站服务器ipteahouse wordpress主题
  • 深州市住房保障和城乡建设局网站做网站公司叫什么
  • 织梦网站转跳手机站注册公司代理记账费用
  • wordpress建站Pdf亚马逊aws在线观看
  • 做网站的外包公司有哪些WordPress调用json数据
  • 做网站网站怎么赚钱网站的建设及维护报告
  • 可以做效果图的网站东莞网站优化什么方法
  • 网站和软件的区别怎么做招生网站
  • 雄安免费网站建设电话如何做网站推广 求指点
  • 十大免费cad网站入口软件北京做网站建设价格
  • 建设一个公司网站要具备什么网页制作的专业
  • 东莞seo建站排名全包网站
  • 网站建设的基本步骤有哪些软件商店下载官方
  • 网站建设开发报告论文hao123上网从这里开始官方
  • 阿里云建网站费用徐州房产网
  • 网站统计分析平台做企业礼品的网站
  • 做可视化图表的网站wordpress批量导入tag
  • txt怎么做网站网站的链接结构包括
  • 适合平面设计师的网站网络营销专员的就业前景