使用场合:

string是C++标准库的一个重要的部分,主要用于字符串处理。可以使用输入输出流方式直接进行操作,也可以通过文件等手段进行操作。同时C++的算法库对string也有着很好的支持,而且string还和c语言的字符串之间有着良好的接口。虽然也有一些弊端,但是瑕不掩瑜。 
其中使用的代码多数都是来自cpp官网,因为例子非常全。

声明和初始化方法:

想使用string首先要在头文件当中加入< string > 
声明方式也很简单

声明:

string s;//声明一个string 对象
string ss[];//声明一个string对象的数组

初始化:

使用等号的初始化叫做拷贝初始化,不使用等号的初始化叫做直接初始化。

#include <bits/stdc++.h>
using namespace std; //注意:如果没有这一行,则需要写成std::string int main()
{
ios::sync_with_stdio(false);
string s;//默认初始化,一个空字符串
string s1("ssss");//s1是字面值“ssss”的副本
string s2(s1);//s2是s1的副本
string s3=s2;//s3是s2的副本
string s4(,'c');//把s4初始化
string s5="hiya";//拷贝初始化
string s6=string(,'c');//拷贝初始化,生成一个初始化好的对象,拷贝给s6 //string s(cp,n)
char cs[]="";
string s7(cs,);//复制字符串cs的前3个字符到s当中 //string s(s2,pos2)
string s8="asac";
string s9(s8,);//从s2的第二个字符开始拷贝,不能超过s2的size //string s(s2,pos2,len2)
string s10="qweqweqweq";
string s11(s10,,);//s4是s3从下标3开始4个字符的拷贝,超过s3.size出现未定义
return ;
}

字符串处理:

substr操作:

注意substr没有迭代器作为参数的操作

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s="abcdefg"; //s.substr(pos1,n)返回字符串位置为pos1后面的n个字符组成的串
string s2=s.substr(,);//abcde //s.substr(pos)//得到一个pos到结尾的串
string s3=s.substr();//efg return ;
}

如果输入的位置超过字符的长度,会抛出一个out_of_range的异常

insert操作:

代码来自cpp官网,经过自己的整理 
注意用迭代器当参数和无符号数当参数的区别

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string str="to be question";
string str2="the ";
string str3="or not to be";
string::iterator it; //s.insert(pos,str)//在s的pos位置插入str
str.insert(,str2); // to be the question //s.insert(pos,str,a,n)在s的pos位置插入str中插入位置a到后面的n个字符
str.insert(,str3,,); // to be not the question //s.insert(pos,cstr,n)//在pos位置插入cstr字符串从开始到后面的n个字符
str.insert(,"that is cool",); // to be not that is the question //s.insert(pos,cstr)在s的pos位置插入cstr
str.insert(,"to be "); // to be not to be that is the question //s.insert(pos,n,ch)在s.pos位置上面插入n个ch
str.insert(,,':'); // to be not to be: that is the question //s.insert(s.it,ch)在s的it指向位置前面插入一个字符ch,返回新插入的位置的迭代器
it = str.insert(str.begin()+,','); // to be, not to be: that is the question //s.insert(s.it,n,ch)//在s的it所指向位置的前面插入n个ch
str.insert (str.end(),,'.'); // to be, not to be: that is the question... //s.insert(it,str.ita,str.itb)在it所指向的位置的前面插入[ita,itb)的字符串
str.insert (it+,str3.begin(),str3.begin()+); // to be, or not to be: that is the question... return ;
}

erase操作:

用来执行删除操作 
删除操作有三种

  • 指定pos和len,其中pos为为起始位置,pos以及后面len-1个字符串都删除
  • 迭代器,删除迭代器指向的字符
  • 迭代器范围,删除这一范围的字符串,范围左闭右开

代码来自cpp官网

#include <iostream>
#include <string> int main ()
{
std::string str ("This is an example sentence.");
std::cout << str << '\n';
// "This is an example sentence."
str.erase (,); // ^^^^^^^^
//直接指定删除的字符串位置第十个后面的8个字符
std::cout << str << '\n';
// "This is an sentence."
str.erase (str.begin()+);// ^
//删除迭代器指向的字符
std::cout << str << '\n';
// "This is a sentence."
// ^^^^^
str.erase (str.begin()+, str.end()-);
//删除迭代器范围的字符
std::cout << str << '\n';
// "This sentence."
return ;
}

append和replace操作:

append函数可以用来在字符串的末尾追加字符和字符串。由于string重载了运算符,也可以用+=操作实现 
repalce顾名思义,就是替换的意思,先删除,后增加。 
代码来自cpp官网,附上自己的解释

#include <iostream>
#include <string> int main ()
{
std::string str;
std::string str2="Writing ";
std::string str3="print 10 and then 5 more"; //直接追加一个str2的字符串
str.append(str2); // "Writing "
//后面追加str3第6个字符开始的3个字符串
str.append(str3,,); // "10 "
//追加字符串形参的前5个字符
str.append("dots are cool",); // "dots "
//直接添加
str.append("here: "); // "here: "
//添加10个'.'
str.append(10u,'.'); // ".........."
//添加str3迭代器范围的字符串
str.append(str3.begin()+,str3.end()); // " and then 5 more"
//最后这个比较特殊,意思是添加5个'A',实际上参数里面的65对应的asc码就是65
str.append<int>(,); // "....."
//字符串追加也可以用重载运算符实现
str+="lalala";
std::cout << str << '\n';
return ;
}

replace的使用方法,replace支持使用无符号整数寻找位置,也支持用迭代器寻找位置

#include <iostream>
#include <string> int main ()
{
std::string base="this is a test string.";
std::string str2="n example";
std::string str3="sample phrase";
std::string str4="useful."; // replace signatures used in the same order as described above: // Using positions: 0123456789*123456789*12345
std::string str=base; // "this is a test string."
//第9个字符以及后面的4个字符被str2代替
str.replace(,,str2); // "this is an example string." (1)
//第19个字符串以及后面的5个字符用str的第7个字符以及后面的5个字符代替
str.replace(,,str3,,); // "this is an example phrase." (2)
//第8个字符以及后面的9个字符用字符串参数代替
str.replace(,,"just a"); // "this is just a phrase." (3)
//第8个字符以及后面的5个字符用字符串参数的前7个字符替换
str.replace(,,"a shorty",); // "this is a short phrase." (4)
//第22以及后面的0个字符用3个叹号替换
str.replace(,,,'!'); // "this is a short phrase!!!" (5)
//迭代器的原理同上
// Using iterators: 0123456789*123456789*
str.replace(str.begin(),str.end()-,str3); // "sample phrase!!!" (1)
str.replace(str.begin(),str.begin()+,"replace"); // "replace phrase!!!" (3)
str.replace(str.begin()+,str.begin()+,"is coolness",); // "replace is cool!!!" (4)
str.replace(str.begin()+,str.end()-,,'o'); // "replace is cooool!!!" (5)
str.replace(str.begin()+,str.end(),str4.begin(),str4.end());// "replace is useful." (6)
std::cout << str << '\n';
return ;
}

以上的replace操作可以用insert和erase的操作组合替换,但是replace操作更加方便。

assign操作: 
assign操作在一起列容器当中都存在,比如vector等等。是一个很基本的操作函数,string使用assign可以灵活的对其进行赋值。 
代码来自cpp官网

#include <iostream>
#include <string> int main ()
{
std::string str;
std::string base="The quick brown fox jumps over a lazy dog."; // used in the same order as described above:
//直接把base赋值给str
str.assign(base);
std::cout << str << '\n';
//把base第10个字符以及后面的8个字符赋给str
str.assign(base,,);
std::cout << str << '\n'; // "brown fox"
//把参数中的0到6个字符串赋给str
str.assign("pangrams are cool",);
std::cout << str << '\n'; // "pangram"
//直接使用参数赋值
str.assign("c-string");
std::cout << str << '\n'; // "c-string"
//给str赋值10个'*'字符
str.assign(,'*');
std::cout << str << '\n'; // "**********"
//赋值是10个'-'
str.assign<int>(,0x2D);
std::cout << str << '\n'; // "----------"
//指定base迭代器范围的字符串
str.assign(base.begin()+,base.end()-);
std::cout << str << '\n'; // "fox jumps over" return ;
}

string的搜索操作:

string类中提供了很多性能优秀,使用方便的成员方法。而且在泛型算法当中也有很多实用的技巧。

find和rfind函数:

find函数主要是查找一个字符串是否在调用的字符串中出现过,大小写敏感。 
代码来自cpp官网

#include <bits/stdc++.h>
using namespace std; int main()
{
ios::sync_with_stdio(false);
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle"); // different member versions of find in the same order as above:
//在str当中查找第一个出现的needle,找到则返回出现的位置,否则返回结尾
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';
//在str当中,从第found+1的位置开始查找参数字符串的前6个字符
found=str.find("needles are small",found+,);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n';
//在str当中查找参数中的字符串
found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n';
//查找一个字符
found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n';
//组合使用,把str2用参数表中的字符串代替
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '\n';
return ;
}

rfind函数就是找最后一个出现的匹配字符串,返回的位置仍然是从前往后数的。

#include <bits/stdc++.h>
using namespace std; int main()
{
ios::sync_with_stdio(false);
std::string str ("The sixth sick sheik's sixth sheep's sick.");
std::string key ("sixth");// ^
//rfind是找最后一个出现的匹配字符串
std::size_t found = str.rfind(key);
if (found!=std::string::npos)
{
cout<<found<<endl;//输出23
str.replace (found,key.length(),"seventh");//找到的sixth替换成seventh
} std::cout << str << '\n';
return ;
}

查找的效率非常高,我没看过stl源码剖析,但是感觉是用kmp实现的。呵呵,可以自己写一个。

find_….of函数:

  • find_first_of(args) 查找args中任何一个字符第一次出现的位置
  • find_last_of(args) 最后一个出现的位置
  • find_fist_not_of(args) 查找第一个不在args中的字符
  • find_last_not_of 查找最后一个不在args中出现的字符
#include <bits/stdc++.h>
using namespace std; int main()
{
ios::sync_with_stdio(false);
std::string str1 ("Please, replace the vowels in this sentence by asterisks.");
std::size_t found1 = str1.find_first_of("aeiou");
//把所有元音找出来用*代替
while (found1!=std::string::npos)
{
str1[found1]='*';
found1=str1.find_first_of("aeiou",found1+);
}
std::cout << str1 << '\n'; //在str2中找到第一个不是消协英文字母和空格的字符
std::string str2 ("look for non-alphabetic characters...");
std::size_t found2 = str2.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
if (found2!=std::string::npos)
{
std::cout << "The first non-alphabetic character is " << str2[found2];
std::cout << " at position " << found2 << '\n';
}
return ;
}

find_last_of和find_last_not_of与first基本相同,就不写例子代码了。

比较与转换:

类似c语言的字符串比较函数strcmp函数一样,支持字符串比较操作,同时也类似python、C#语言中的函数一样,支持把数字和字符串转换。有些特性是C++11当中才有。 
注意编译器bug: 
在MinGW编译器当中如果版本低于3.8,虽然支持c++11但是里面有一个bug,就是不支持字符串和数组的转换!要更新MinGW的版本才可以,或者直接使用g++。

compare函数:

和strcmp函数一样,如果两个字符串相等,那么返回0,调用对象大于参数返回1,小于返回-1。 
在compare当中还支持部分比较,里面有6个参数可以设置。

#include <bits/stdc++.h>
using namespace std; int main()
{
ios::sync_with_stdio(false);
string s1="",s2="";
cout<<s1.compare(s2)<<endl;// s1="",s2="";
cout<<s1.compare(s2)<<endl;//-1 s1="",s2="";
cout<<s1.compare(s2)<<endl;// std::string str1 ("green apple");
std::string str2 ("red apple"); if (str1.compare(str2) != )
std::cout << str1 << " is not " << str2 << '\n';
//str1的第6个字符以及后面的4个字符和参数比较
if (str1.compare(,,"apple") == )
std::cout << "still, " << str1 << " is an apple\n"; if (str2.compare(str2.size()-,,"apple") == )
std::cout << "and " << str2 << " is also an apple\n";
//str1的第6个字符以及后面的4个字符和str2的第4个字符以及后面的4个字符比较
if (str1.compare(,,str2,,) == )
std::cout << "therefore, both are apples\n";
return ;
}

由于string重载了运算符,可以直接用>,<,==来进行比较,也很方便。

数值转换:

在io的部分有过数值和字符串相互转换的例子,使用的是stringstream函数,在c++11当中有定义好的现成的函数取调用,非常方便。

string和数值转换  
to_string(val) 把val转换成string
stoi(s,p,b) 把字符串s从p开始转换成b进制的int
stol(s,p,b) long
stoul(s,p,b) unsigned long
stoll(s,p,b) long long
stoull(s,p,b) unsigned long long
stof(s,p) float
stod(s,p) double
stold(s,p) long double

//注意,下段代码在MinGw中会报错!即使使用c++11编译也一样,无法识别to_string!

#include <bits/stdc++.h>
using namespace std; int main()
{
ios::sync_with_stdio(false);
string s1;
s1=to_string();
cout<<s1<<endl;
int a=stoi(s1,,)+;
cout<<a<<endl; return ;
}

[UE4]C++ string的用法和例子的更多相关文章

  1. C++ string的用法和例子

    使用场合: string是C++标准库的一个重要的部分,主要用于字符串处理.可以使用输入输出流方式直接进行操作,也可以通过文件等手段进行操作.同时C++的算法库对string也有着很好的支持,而且st ...

  2. C++【string】用法和例子

    /*** * string 基础api复习 * 8 AUG 2018 */ #include <iostream> #include <string> using namesp ...

  3. 我教女朋友学编程html系列(5) html中table的用法和例子

    女朋友不是学计算机的,但是现在从事计算机行业,做技术支持,她想学习编程,因此我打算每天教她一点点,日积月累,带她学习编程,如果其他初学者感兴趣,可以跟着学. 为了将table介绍的简单.生动,具有实战 ...

  4. [UE4] C++实现Delegate Event实例(例子、example、sample)

    转自:http://aigo.iteye.com/blog/2301010 虽然官方doc上说Event的Binding方式跟Multi-Cast用法完全一样,Multi-Cast论坛上也有很多例子, ...

  5. CodeSmith 基础用法和例子

    〇.            前言 一.            工具设置 CodeSmith默认是不支持中文的,那么我们必须要先设置使其支持中文显示,保存.并且要能够在生成文件中支持中文. [Tools ...

  6. String Aop 动态代理例子

    动态代理原理:spring AOP采用动态代理来实现 (1)定义一个接口Boy package aop001; public interface Boy { public void beat(Stri ...

  7. sqlMetal用法和例子 自定义DBML

    SqlMetal是跟随VS发布的一个自动工具,可以用来生成数据库的Linq代码. 这是中文版的帮助文件. SqlMetal [选项] [<输入文件>] 为 .NET Framework 的 ...

  8. 关于String.concat()方法和StringBuffer.append()方法的学习:方法是如何追加字符到源字符串的

    问题分析: 首先,看看两段代码的运行结果,两段代码分别是: 第一段代码,关于String.concat()方法的测试: public static void main(String[] args) { ...

  9. C# String.Format用法和格式说明

    1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...

随机推荐

  1. Java中的容器和注入分析

    为什么会出现容器的注入? 容器:顾名思义,装东西的器物. 至于spring中bean,aop,ioc等一些都只是实现的方式:具体容器哪些值得我们借鉴,我个人觉得是封装的思想.将你一个独立的系统功能放到 ...

  2. [追加评论]三款SDR平台对比:HackRF,bladeRF和USRP

    这三个月,有幸把3种板子都用到了.说说使用体会.   我用过其中的HackRF,bladeRF x115,USRP B210.我并没有仔细的测量各种板子的射频指标什么的,只是做各种实验的时候用到它们. ...

  3. UDP广播与多播

    UDP广播与多播 使用UDP协议进行信息的传输之前不需要建议连接.换句话说就是客户端向服务器发送信息,客户端只需要给出服务器的ip地址和端口号,然后将信息封装到一个待发送的报文中并且发送出去.至于服务 ...

  4. SWIFT用ScrollView加图片制作Banner

    网上参考OBJC写的用ScrollView图片轮播效果,照着画了个,先上效果图: 附上代码: @IBOutlet weak var pc: UIPageControl! @IBOutlet weak ...

  5. 如何在win10(64位系统)上安装apache服务器

    今天装了Apache服务器,下面是我总结的方法: 一,准备软件 1.64位的apache版本 传送门:http://www.apachelounge.com/download/ 2.VC11运行库 下 ...

  6. C高级第三次作业(1)

    6-1 输出月份英文名 1.设计思路: 1.定义一个字符串数组将12个月的英文加进去: 2.判断输入的数是否大于等于1小于等于12: 3.若是 则返还s[n-1]; 4.否则返还NULL: 源代码: ...

  7. 批量分割视频opencv

    前言 视频处理过程中,会用到对等长的视频进行处理,此时要对大视频进行分割. 实现步骤 1.批量读取视频集: 2.视频分割: 测试代码 1.批量读取视频集: /********************* ...

  8. Photoshop通道抠出散乱的儿童头发

    抠图之前仔细分析是必不可少的.要了解清楚需要抠取部分的构成,然后选择最快捷的方法.教程素材图片人物头发色调比较单一,背景色也比较单一,用通道抠图是非常快捷的. 最终效果1 最终效果2 原图 一.复制图 ...

  9. ubuntu12.04 alternate win7 双系统安装

    ubuntu alternate的安装比desktop复杂一点,因为alternate的安装过程有个步骤是检测cd-rom,如果你是刻盘安装,自然没问题,但是,现在的安装一般是将系统刻到U盘里,或者在 ...

  10. Hash表的平均查找长度ASL计算方法

    Hash表的“查找成功的ASL”和“查找不成功的ASL” ASL指的是 平均查找时间 关键字序列:(7.8.30.11.18.9.14) 散列函数: H(Key) = (key x 3) MOD 7 ...