博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
string中常用的函数
阅读量:6857 次
发布时间:2019-06-26

本文共 1909 字,大约阅读时间需要 6 分钟。

string中常用的函数

发现在string在处理这符串是很好用,就找了一篇文章放在这里了..

用 string来代替char * 数组,使用来排序,用来去重

1、Define
           string s1 = "hello";
           string s2 = "world";
           string s3 = s1 + "," + s2 +"!\n";
2、append
           s1 += ",shanshan\n";
3、Compare
           if(s1 == s2)
              .....
           else if(s1 == "hello")
              .....
4、 string 重载了许多操作符,包括 +, +=, <, =, , [], <<, >>等,正式这些操作符,对字符串操作非常方便

#include 
#include
using namespace std; int main(){
           string strinfo="Please input your name:";            cout << strinfo ;            cin >> strinfo;         if( strinfo == "winter" )            cout << "you are winter!"<
5、find函数
由于查找是使用最为频繁的功能之一,string 提供了非常丰富的查找函数。其列表如下:
函数名 描述
find 查找
rfind 反向查找
find_first_of 查找包含子串中的任何字符,返回第一个位置
find_first_not_of 查找不包含子串中的任何字符,返回第一个位置
find_last_of 查找包含子串中的任何字符,返回最后一个位置
find_last_not_of 查找不包含子串中的任何字符,返回最后一个位置
以上函数都是被重载了4次,以下是以find_first_of 函数为例说明他们的参数,其他函数和其参数一样,也就是说总共有24个函数:
size_type find_first_of(const basic_string& s, size_type pos = 0) size_type find_first_of(const charT* s, size_type pos, size_type n) size_type find_first_of(const charT* s, size_type pos = 0) size_type find_first_of(charT c, size_type pos = 0) 所有的查找函数都返回一个size_type类型,这个返回值一般都是所找到字符串的位置,如果没有找到,则返回string::npos。 其实string::npos表示的是-1。即没找到就返回-1。例子如下: #include 
#include
using namespace std; int main(){
           string strinfo="      //*---Hello Word!......------";            string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";         int first = strinfo.find_first_of(strset);         if(first == string::npos) {                    cout<<"not find any characters"<
#include
using namespace std; int main() {
           string strinfo="This is Winter, Winter is a programmer. Do you know Winter?";            cout<<"Orign string is :\n"<
<

转载地址:http://oojyl.baihongyu.com/

你可能感兴趣的文章
dd简单用法
查看>>
关于c语言的赋值和memcpy的问题
查看>>
Android----Android之TextView实现文字过长时省略部分或者滚动显示 (跑马灯)
查看>>
SCVMM 2012 SP1 安装
查看>>
javascript页面刷新的几种方法
查看>>
“中国互联网100强”(2013)发布
查看>>
判断当前屏幕的方向
查看>>
转 Zend Studio 10正式版注册破解
查看>>
JUnit入门
查看>>
看完《超级演说家》之后
查看>>
(转)NGUI中深度depth和z轴关系
查看>>
[.net 面向对象编程基础] (17) 数组与集合
查看>>
android和ios,音频互通方案
查看>>
JavaScript--DOM修改元素的属性
查看>>
object-c输出对象
查看>>
#include <algorithm>中sort的一般用法
查看>>
hibernate(一)第一个例子
查看>>
归并排序(Merge Sort)
查看>>
Cesium原理篇:7最长的一帧之Entity(上)
查看>>
[linux]为阿里云ECS(CentOS7)配置IPv6地址
查看>>