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

灌云网站建设维护手机网站建站步骤论文

灌云网站建设维护,手机网站建站步骤论文,app研发,xx市院门户网站建设方案一、string概念 之前介绍过通过字符数组保存字符串#xff0c;然后对字符数组中的字符串做各种操作#xff1b;为了更加简单方便#xff0c;在C中#xff0c;又增加了 string 来处理字符串。 char str[20] hello world; string 字符串其实是一种更加高级的封… 一、string概念 之前介绍过通过字符数组保存字符串然后对字符数组中的字符串做各种操作为了更加简单方便在C中又增加了 string 来处理字符串。 char str[20] hello world; string 字符串其实是一种更加高级的封装 string 字符串中包含大量的方法这些方法使得字符串的操作变得更加简单。那到底什么是string呢C中将字符串直接作为一种类型也就是 string 类型使用  string 类型创建的对象就是C 的字符串。 string s1; string s2 abc; 使用 C 中提供的 string 时必须添加头文件 string。 二、string 常见操作 1. 创建字符串 string s1;//创建空字符串 string s2 abc;//创建字符串 #include iostream #include string //添加string头文件 using namespace std; int main() {string s1;string s2 hello world;cout s1: s1 endl; //s1:cout s2: s2 endl; //s2hello worldreturn 0; } 创建字符串的方式与前面学习到创建内置数据类型的方式相同只是这里的字符串类型为 string 。 1string s1 表示创建空字符串相当于创建整型 int a 但未给 a 一 个初始值。 2 string s2 hello world 表示创建一个字符串s2它的内容是 hello world 要 注意 s2 中的字符串不再以 \0 作为结束标志了。C 语言风格的字符串是以 \0 作为结束标志 的后面还会介绍别的创建字符串的方法但这种方法是最常见的。 上面这个图仅仅是字符串s2的示意图实际上 string 类型的字符串比这个要复杂的多我们可以大概这样去理解更多的知识需要学习C的类和对象的知识才能明白。 当然C中的 string 创建的字符串和 char 类型的数组所表示的字符串还有一个区别 string 类型的字符串对象可以直接赋值比如 #include iostream #include string using namespace std; int main() {string s1(hello world);string s2(hehe);s2 s1;cout s2 endl;return 0; } 除了以上创建字符串的写法外C中还有一些其他的创建字符串方式。如 string s(hello world); //等同于string s1 hello world; string s1 s; //⽤⼀个现成的字符串s初始化另外⼀个字符串s1 2. string字符串的输入 1 cin的方式 可以直接使用  cin 给 string 类型的字符串中输入一个字符串的数据。 #include iostream #include string using namespace std; int main() {string s;//输入 cin s;//输出cout s endl;return 0; } 其实 cin 的方式给 string 类型的字符串中输入数据的时候可以输入不带空格的字符串。但是如果带有空格遇到空格也就读取结束了没有办法正常读取那怎么办呢解决办法就是使用  getline 。 2 getline 的方式 getline 是C标准库中的一个函数用于从输入流中读一行文本并将其存储为字符串。getline 函数有两种不同的形式分别对应着字符串的结束方式。 istream getline (istream is, string str); istream getline (istream is, string str, char delim); istream 是输入流类型 cin 是 istream 类型的标准输入流对象。 ostream 是输出流类型 cout 是 ostream 类型的标准输出流对象。 getline 函数是输入流中读取一行文本信息所有如果是在标准输入流键盘中读取数据就可以传 cin 给第一个参数。 第一种 getline 函数以换行符 \n 作为字符串的结束标志它的一般格式是: getline(cin, string str) //cin -- 表⽰从输⼊流中读取信息 //str 是存放读取到的信息的字符串 这种形式的 getline 函数从输入流 例如 cin 中 读取文本直到遇到换行符 \n 为止然后将读取到的文本不包括换行符存储到指定的 string 类型的变量 str 中。 #include iostream #include string using namespace std; int main () {string name;getline (cin, name);cout name endl;return 0; } 第二种 getline 函数允许用户自定义结束标志它的一般格式是: getline(cin, string str, char delim) //cin -- 表⽰从输⼊流中读取信息 //str 是存放读取到的信息的字符串 //delim 是⾃定义的结束标志 这种形式的 getline 函数从输入流中读取文本直到遇到用户指定的结束标志字符 delim 为止然后将读取到的文本不包括结束标志字符存储到指定的 string 类型的变量 str 中。 #includeiostream #include string using namespace std; int main () {string name;getline (cin, name, q);cout name endl;return 0; } 在使用C中的 string 字符串时想要输入的字符串中包含空格那么 getline 函数就是必须的。在竞赛中为了方便处理字符串通常会使用 string 类型的字符串所以在字符串输入的时候 getline 就很常见。 3. size() string 中提供了 size() 函数用于获取字符串长度。 使用示例 #include iostream #include string //添加string头⽂件 using namespace std; int main() {string s;string s1 hello;string s2 hello world;string s3 12ab!~ ; cout s: s.size() endl;cout s1: s1.size() endl;cout s2: s2.size() endl;cout s3: s3.size() endl;return 0; } 需要注意的是像 char 、 int 、 double 等内置类型的数据在操作的时候我们是不会使用 . 操作符的。string 是 C提供的一种更加复杂的封装类型在 string 类型的变量中加入了操作这个字符串的各种方法函数, 比如求字符串长度、字符串末尾插入⼀个字符等操作。所以要对 string 类型的变量进行各种操作就可以使用  . 操作符来使用这些函数。 我们可以看下⾯的例⼦⼤家要仔细体会。 通过 size() 能获得字符串的长度顺便就可以使用这个长度遍历字符串的注意 string 类型的字符串是可以通过下标访问的比如 #incldue iostream #include string using namespace std; int main() {string s abcdef;int i 0;for(i 0; i s.size(); i){cout s[i] ;}return 0; } 4.迭代器(iterator) 迭代器是一种对象它可以用来遍历容器比如 string 中的元素迭代器的作用类似于指针或者数组下标。访问迭代器指向的值需要解引用(*)。 C 中的 string 提供了多种迭代器用于遍历和操作字符串中的内容。这里给大家介绍一种最常 用的迭代器。 begin() 和 end() begin() 返回指向字符串第一个字符的迭代器需要一个迭代器的变量来接收。 end() 返回指向字符串最后一个字符的下一个位置的迭代器该位置不属于字符串。 string 中 begin() 和 end() 返回的迭代器的类型是 string::iterator 。 迭代器是可以进行大小比较也可以进行  或者 - 整数运算的。 比如 it 就是让迭代器前进一步 it-- 就是让迭代器退后一步。 同一个容器的两个迭代器也可以相减相减结果的绝对值是两个迭代器中间元素的个数。 #include iostream #include string using namespace std; int main() {string s abcdef;string::iterator it1 s.begin();string::iterator it2 s.end();cout (it1 it2) endl;cout it1 - it2 endl;return 0; } 迭代器通常用于遍历字符串的可以正序遍历也可以逆序遍历。 正序遍历 #include iostream #include string using namespace std; int main() {string s abcdef; //auto it 是让编译器⾃动推到it的类型for (auto it s.begin(); it ! s.end(); it) { cout *it ; }//string::iterator 是正向迭代器类型//string::iterator it是直接创建迭代器it是针对字符串的迭代器for (string::iterator it s.begin(); it ! s.end(); it) { cout *it ; }return 0; } 逆序遍历 #include iostream #include string using namespace std; int main() {string s abcdef; for (string::iterator it s.end() - 1; it s.begin(); --it) { cout *it ; }return 0; } 通过迭代器找到元素后改变迭代器指向的元素是可以直接改变字符串内容的。 #include iostream #include string using namespace std; int main() {string str abcdef; cout str endl;for (string::iterator it str.begin(); it ! str.end(); it) { *it x; }cout str endl;return 0; } 5.push_back() push_back() 用 于在字符串尾部插一个字符。 使用示例 #include iostream #includestring //添加string头⽂件 using namespace std; int main() {//向空字符串中尾插字符string s;s.push_back(h);s.push_back(e);s.push_back(l);s.push_back(l);s.push_back(o);cout s endl;//向⾮空字符串中尾插字符string s1 hello ;s1.push_back(w);s1.push_back(o);s1.push_back(r);s1.push_back(l);s1.push_back(d);cout s1 endl;//批量插⼊字符string s2;for (char c a; c f; c){s2.push_back(c);}cout s2 endl;return 0; } 6.字符串的和运算 push_back() 是用于在字符串后添加一个字符然而部分情况下我们需要向原有的字符串后继续添 加字符串。 其实 string 类型的字符串是支持 和 运算的。这里的本质是 string 中重载了operator 这个操作符。 具体使用请看下面的示例 #include iostream #include string //添加string头⽂件 using namespace std; int main() {string s hello;s world; //字符串⽤双引号等价于 s s worldcout s endl;//除了操作也可以使⽤灵活进⾏字符串拼接//1.尾部拼接string s1 hello;cout s1 world endl; //s1仍然是hello s1 s1 world;cout s1 endl; //s1是hello world //2.头部拼接string s2 hello;s2 world s2 ; cout s2 endl; //s2为world hello return 0; } 7.pop_back() pop_back() 用 于删除字符串中尾部的一个字符。这个成员函数是在 C11 标准中引入的有的编 译器可能不支持。 使用示例 #include iostream #includestring using namespace std; int main() {string s hello;cout s: s endl;//尾删s.pop_back();cout s: s endl;//尾删s.pop_back();cout s: s endl;return 0; } 但是当字符串中没有字符的时候再调用  pop_back() 时程序会出现异常。这种行为也是未定义 行为要避免使用。 #include iostream #include string //添加string头⽂件 using namespace std; int main() {string s;s.pop_back();return 0; } 在DevC上程序最终崩溃了。 为避免循环删除导致对空字符串进行尾删可以将删除全部字符的代码写成 #include iostream #includestring //添加string头⽂件 using namespace std; int main() {string s abc;while(s.size() 0) //通过size()函数来控制字符串的⻓度{s.pop_back();}return 0; } 8.insert 如果我们需要在字符串中间的某个位置插入一个字符串怎么办呢这时候我们得掌握一个函数就是 insert。 函数原型如下 string insert (size_t pos, const string str); //pos位置前⾯插⼊⼀个string字符串 string insert (size_t pos, const char* s); //pos位置前⾯插⼊⼀个C⻛格的字符串 string insert (size_t pos, size_t n, char c);//pos位置前⾯插⼊n个字符c 代码举例 #include iostream #include string using namespace std; int main() {string s abcdefghi;string str xxx;cout s endl;s.insert(3, str);cout s endl;return 0; } #include iostream #include string using namespace std; int main() {string s abcdefghi;cout s endl;s.insert(3, xxx);cout s endl;return 0; } #include iostream #include string using namespace std; int main() {string s abcdefghi;cout s endl;s.insert(3, 3, x);cout s endl;return 0; } 9.find() find() 函数用于查找字符串中指定子串/字符并返回子串/字符在字符串中第一次出现的位置。 size_t find (const string str, size_t pos 0) const; //查找string类型的字符串str默认是从头开始查找pos可以指定位置开始 size_t find (const char* s, size_t pos 0) const; //查找C⻛格的字符串s默认是从头开始查找pos可以指定位置开始 size_t find (const char* s, size_t pos, size_t n) const; //在字符串的pos这个位置开始查找C⻛格的字符串s中的前n个字符 size_t find (char c, size_t pos 0) const; //查找字符c默认是从头开始pos可以指定位置开始 返回值 若找到返回子串/字符在字符串中第一次出现的起始下标位置。 若未找到返回一个整数 值 npos 针对 npos 的介绍 会在下面给出。通常判断 find() 函数的返回值是否等于 npos 就能知道是否查找到子串或者字符。 代码举例 //代码1 #include iostream #include string //添加string头⽂件 using namespace std; int main() {string s hello world hello everyone;string str llo;//查找string类型的字符串size_t n s.find(str);//省略第二个参数默认为0从头开始cout n endl;n s.find(str, n 1); //从n1这个指定位置开始查找cout n endl;//查找C⻛格的字符串n s.find(llo);//省略第二个参数默认为0从头开始cout n endl;n s.find(llo, n 1); //从n1这个指定位置开始查找cout n endl;return 0; } //代码2 #include iostream #include string //添加string头⽂件 using namespace std; int main() {string s hello world hello everyone;//在s中0这个指定位置开始查找word中的前3个字符size_t n s.find(word, 0, 3);cout n endl;n s.find(everyday, n1, 5);cout n endl; return 0; } //代码3 #include iostream #include string //添加string头⽂件 using namespace std; int main() {string s hello world hello everyone;size_t n s.find(o);cout n endl;n s.find(o, n 1);cout n endl;return 0; } //查找不到的情况 #include iostream #include string //添加string头⽂件 using namespace std; int main() {string s hello world hello everyone;string str bit;size_t n s.find(str);cout n endl;if(n ! string::npos)cout 找到了位置是 n endl;elsecout 没有找到 endl;return 0; } 在字符串中查找字符或者字符串时有可能查找不到这时候 find 函数会返 回 npos 这个值 该数 字并不是一个随机的数字而是 string 中定义的一个静态常量 npos 。我们通常会判断 find 函数的返回值是否等于 npos 来判断查找是否成功。 static const size_t npos -1; 打印出来看看 #include iostream #include string //添加string头⽂件 using namespace std; int main() {//注意npos是string中定义的使⽤npos需要带上string::指明是string类中的cout npos: string::npos endl; //npos:18446744073709551615return 0; } 10.substr() substr() 函数用于截取字符串中指定位置指定长度的子串。函数原型如下 string substr (size_t pos 0, size_t len npos) const; //pos 的默认值是0也就是从下标为0的位置开始截取 //len 的默认值是npos意思是⼀直截取到字符串的末尾 substr() :如果函数不传参数就是从下标为0的位置开始截取直到结尾得到的是整个字符串 substr(pos) 从指定下标 pos 位置开始截取子串直到结尾 substr(pos, len) 从指定下标 pos 位置开始截取长度为 len 的子串。 返回值类型 string 返回的是截取到的字符串可以使用  string 类型的字符串接收。 代码举例 #include iostream #includestring //添加string头⽂件 using namespace std; int main() {string s hello world hello everyone;string s1 s.substr(7);cout s1 endl;string s2 s.substr(7, 6);cout s2 endl;return 0; } substr() 和 find() 经常是配合使用的 find 负责找到位置 substr 从这个位置向后获得字符串。 #include iostream #includestring //添加string头⽂件 using namespace std; int main() {string s hello world hello everyone;size_t n s.find(world);string s2 s.substr(n, 10);cout s2 endl;return 0; } 11.string的关系运算 在实际写代码的过程中经常会涉及到两个字符串比较大小比如判断你输入的密码是否正确就得将输入的密码和数据库中正确的密码比较。 那么两个 string 类型字符串是否可以比较大小呢其实是可以的C中为string提供了一系列的关系运算。 支持的关系运算 string s1 abc; string s2 abcd; char s3[] abcdef; //C⻛格的字符串 (1) s1 s2 bool operator (const string lhs, const string rhs);//使⽤⽅式s1 s2 bool operator (const char* lhs, const string rhs);//使⽤⽅式s3 s1 bool operator (const string lhs, const char* rhs);//使⽤⽅式s1 s3 (2) s1 ! s2 bool operator! (const string lhs, const string rhs);//使⽤⽅式s1 ! s2 bool operator! (const char* lhs, const string rhs);//使⽤⽅式s3 ! s1 bool operator! (const string lhs, const char* rhs);//使⽤⽅式s1 ! s3 (3) s1 s2 bool operator (const string lhs, const string rhs);//使⽤⽅式s1 s2 bool operator (const char* lhs, const string rhs);//使⽤⽅式s3 s1 bool operator (const string lhs, const char* rhs);//使⽤⽅式s1 s3 (4) s1 s2 bool operator (const string lhs, const string rhs);//使⽤⽅式s1 s2 bool operator (const char* lhs, const string rhs);//使⽤⽅式s3 s1 bool operator (const string lhs, const char* rhs);//使⽤⽅式s1 s3 (5) s1 s2 bool operator (const string lhs, const string rhs);//使⽤⽅式s1 s2 bool operator (const char* lhs, const string rhs);//使⽤⽅式s3 s1 bool operator (const string lhs, const char* rhs);//使⽤⽅式s1 s3 (6) s1 s2 bool operator (const string lhs, const string rhs);//使⽤⽅式s1 s2 bool operator (const char* lhs, const string rhs);//使⽤⽅式s3 s1 bool operator (const string lhs, const char* rhs);//使⽤⽅式s1 s3 上述的关系运算符的重载看着复杂但是使用起来是非常简单的。 关于操作符的重载只有深入学习C的类和对象才能深入理解和掌握在竞赛中不需要深入挖掘会使用就行但是要使用C进行工程性开发的时候这部分知识一定要补上。 注字符串的比较是基于字典序进行的比较是对应位置上字符的ASCII值的大小比较的不是字符串的长度。 比如 abc aq //b的ascii码值是⼩于q的 abcdef ff //a的ASCII码值是⼩于f的 100 9 //1的ASCII码值是⼩于9的 代码举例1 #include iostream #includestring using namespace std; int main() {string s1 hello world;string s2 hello;if (s1 (s2 world)) {cout s1 s2 endl;}else{cout s1 ! s2 endl;}return 0; } 代码举例2 #include iostream #include string using namespace std; int main() {string s1 abcd;string s2 abbcdef;char s3[] bbc;if (s1 s2)cout s1 s2 endl;elsecout s1 s2 endl;if (s1 s2)cout s1 s2 endl;elsecout s1 ! s2 endl;if (s1 s3)cout s1 s3 endl;elsecout s1 s3 endl;return 0; } 12.和string相关的函数 (1stoi/stol stoi 是将字符串转换成 int 类型的值 stol 是将字符串转换成 lon g int 类型的值 这里以 stoi 为例讲解一下这两函数的使用方式。 stoi 函数其实可以将一个 string 类型的字符串转化为整型函数原型如下 int stoi (const string str, size_t* idx 0, int base 10); long stol (const string str, size_t* idx 0, int base 10); str 表示被转换的 string 类型的字符串 idx 是一个输出型参数也就是这个通过这个参数会带回一个值。 idx 是一个指针需要在外边创建一个 size_t 类型的值传递它的地址给 idx 这个参数将会带回 str 中无法正确匹配数字的第一个字符的位置。如果不想使用这个参数可以将这个参数设为NULL或0。 base 表示被解析的字符串中数字的进制值可能是 2 , 8 , 10 , 16 或者 0 。 默认情况下这个值是 10 表示  10 进制数字 如果传递的是 2 表示被解析的字符串中是 2 进制的数字最终会转换成 10 进制 如果传递的是 8 表示被解析的字符串中是 8 进制的数字最终会转换成 10 进制 如果传递的是 16 表示被解析的字符串中是 16 进制的数字最终会转换成 10 进制 如果传递的是 0 会根据字符串的内容的信息自动推导进制比如字符串中有 0x 就认为 是 16 进制 0 开头会被认为是 8 进制最终会转换成 10 进制。 代码举例 #include iostream #includestring using namespace std; int main() {size_t pos 0;string s1 11x34;int ret1 stoi(s1, pos, 16);cout ret1 endl;cout pos: pos endl;string s2 11x34;int ret2 stoi(s2, pos, 2);cout ret2 endl;cout pos: pos endl;string s3 0x11x34;int ret3 stoi(s3, pos, 0);cout ret3 endl;cout pos: pos endl;return 0; } 2stod/stof stod 是将字符串转换成 double 类型的值函数原型如下和 stoi 函数的比较的话少了描述 字符串中数字进制的参数其他参数一致。 stof 是将字符串转换成 flaot 类型的值。 double stod (const string str, size_t* idx 0); float stof (const string str, size_t* idx 0); 代码举例 #include iostream #includestring using namespace std; int main() {string s 3.14x456;double ret stod(s, NULL);cout ret endl;return 0; } 3to_string 函数原型如下 string to_string (int val); string to_string (long val); string to_string (long long val); string to_string (unsigned val); string to_string (unsigned long val); string to_string (unsigned long long val); string to_string (float val); string to_string (double val); string to_string (long double val); tostring 函数可以将数字转换成字符串从上述函数原型的角度看的话可以将整型、浮点型的数 字转换成字符串的使用起来也非常简单。 代码举例 #include iostream #include string using namespace std; int main() {string pi pi is to_string(3.14159);cout pi endl;return 0; }
http://www.pierceye.com/news/443274/

相关文章:

  • 各大网站平台发布信息山亭网站建设
  • 做网站.服务器怎么买公司网站如何上传视频
  • 广州建设工程造价信息网长春百度网站优化
  • 郑州外贸网站建设公司价格wordpress禁止百度抓取
  • 临沂建站程序衡阳网站建设ss0734
  • 开发软件下载网站备案号放网站下面居中
  • 开封网站网站建设有哪些好的模板网站
  • 专业做蛋糕视频网站网站目录怎么做301跳转
  • 白城网站建设网络营销顾问培训
  • 沈阳网站开发培训多少钱百度收录批量提交入口
  • php做的网站怎么入侵wordpress插件安装教程
  • 网站 免费 认证58同城东莞招聘
  • 大兴网站建设服务公司石家庄建站
  • 怎么给公司做个网站wordpress h1标签
  • 电子商务网站设计的原则wordpress 图片 不显示缩略图
  • 网站设计制作开发更改网站名称
  • 兰州构建公司优化网站佛山厂商
  • 外贸网站建设需要多少钱it行业软件开发
  • 手机网站开发哪个好兰州哪家网站做推广效果好
  • 南宁定制建站学生做义工网站
  • 开阳县城乡建设局网站sae 部署wordpress
  • 360免费建站怎么样修改网站图标
  • 心理咨询网站模板国税网站页面建设中
  • 网站查询工信部深圳保障性住房统一网
  • 个人网站建设的目的免费编程软件下载
  • 潍坊网站建设优化推广彩页设计公司
  • 海洋网站建设网络钓鱼网站链接
  • 网站界面设计尺寸规范wordpress清理网站缓存
  • ios开发者网站生鲜电商网站建设
  • 域名网站备案查询行业网站名称