您的位置:首页 > 编程语言 > C语言/C++

ACM学习历程9——string基本字符系列容器

2016-08-24 20:09 295 查看
在C++中string提供了字符串的添加、删除、替换、查找等丰富的算法,使用时是需要包含<string>头文件即可,因此在需要存储字符串的情况和需要对字符串进行操作时可以考虑使用string类型的变量。下面对string的相关定义和主要操作如下:
(一)定义:string s;此时s是一个空串,长度为0;
(二)string类型变量的赋值:
(1)直接赋值:
 string s;
 s=”hello world”;
(2)用字符指针赋值:
 string s;   char ss[5000];
 scanf(“%s”,ss);
 s=ss;
(三)在尾部添加字符串或者字符:
(1)尾部添加一个字符,用“+”
 string s;
 s=s+’a’;
(2)尾部添加一个字符串(两种方式)
    用“+”方式添加:
    string s;
    s=s+”abc”;
    用append()方法添加:
    string s;
    s=s.append(“abc”);
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
string s="test";
//在尾部添加一个字符
s=s+'1';
cout<<s<<endl;
//尾部添加字符串(直接添加)
s=s+"234";
cout<<s<<endl;
//尾部添加字符串(append方式添加)
s.append("678");
cout<<s<<endl;
return 0;
}
(四)在某一位置插入元素:
insert(位置,char);其中,位置用迭代器表示,不是下标。
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s="123";
string ::iterator pos=s.begin();
pos++;
s.insert(pos,'4');
cout<<s<<endl;
return 0;
}
(五)访问:
(1)整体访问:string s=”123”;   cout<<s<<endl;

(2)逐个访问,使用下标[ ]

string s;

s[0]

(3)string对象作为vector元素时的访问(P29)

       vector<string> v;

       v[0],v[0][0]

(六)删除:

(1)erase(位置):删除单个元素;

(2)erase(位置1,位置2)
:删除区域内的多个元素,其中,位置用迭代器表。示,不是下标。

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
string s="1234567";
string ::iterator pos=s.begin();
//删除单个元素
s.erase(s.begin());
cout<<s<<endl;
//删除区间元素
s.erase(s.begin(),s.end());
cout<<endl;
return 0;
}

(七)求字符串长度/判空:

(1)length()

(2)empty():为空返回1,不为空返回0

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
string s="1234567";
//字符串长度:
cout<<s.length()<<endl;
//判断字符串是否为空
cout<<s.empty()<<endl;
return 0;
}
(八)替换:replace(位置,长度,替换字符串),此处位置是下标。

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
string s="1234567";
s.replace(1,1,"abc");
cout<<s<<endl;
return 0;
}
(九)查找字符或者字符串:

Find()函数,查找一个字符char或一个子串string,若找到返回找到位置的下标,若没有找到则返回一个值string::npos。

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
string s="1234567";
int pos=s.find("234");
if(pos!=string::npos)
cout<<"found"<<endl;
else
cout<<"not found"<<endl;
return 0;
}
(十)字符串比较:

比较函数 compare(),实际上是按照ASCII的大小比较各个字符串,若两个字符串相等返回0,若第一个字符串大于第二个字符串返回1,反之返回-1。

(十一)algorithm中提供的算法:

(1)反向排列算法:reverse(位置1,位置2);

(2)排序算法:sort(位置1,位置2,比较函数)


(3)计数算法:count(位置1,位置2,字符)。

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
string s="1234567";
//reverse反向排列
reverse(s.begin(),s.end());
cout<<s<<endl;

string s1="523571";
//sort排序
sort(s1.begin(),s1.end());
cout<<s1<<endl;

string s2="102010230";
//count函数统计字符的个数,例如统计1的个数
int n=count(s2.begin(),s2.end(),'1');
cout<<n<<endl;
return 0;
}
(十二)string与char数组:

(1)char数组
char ss[20]

输入:scanf(“%s”,ss);  cin>>ss;

输出: printf(“%s”,ss);  cout<<ss;

(2)string对象,string s

输出:s=ss;printf(s.c_str());  cout<<s;

输入:scanf(“%s”,ss); s=ss; cin>>s;

注:成员函数c_str()用于获取stirng所对应的C语言的数组;若事先定义了string类型的变量,对该变量按照C中字符串的输入方式赋值,则必须在赋值之前为该变量分配适当的内存空间,例如:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
string s;
s.resize(10);
scanf("%s",s.c_str());
printf("%s\n",s.c_str());
return 0;
}
(十三)分离string中的子串:

sscanf(string ,”格式说明”, 变量地址表列);

#include <stdio.h>
int main ()
{
char sentence []="Tom is 12 years old";
char str [20];
int i;
sscanf (sentence,"%s %*s %d",str,&i);
printf ("%s -> %d\n",str,i);
return 0;
}
输出结果:Tom -> 12
sprintf(string ,”格式说明”, 变量地址表列);

#include <stdio.h>
int main ()
{
char buffer [50];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a %d char string\n",buffer,n);
return 0;
}
[5 plus 3 is 8] is a 13 long string
(十四)string与数值的相互转换

string convertToString(double );

double convertFromString(string);

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

string converToString(double x)
{
ostringstream o;
if(o<<x)
return o.str();
return "conversion error";
}

double converFromString(const string &s)
{
istringstream i(s);
double x;
if(i>>x)
return x;
return 0.0;
}
int main()
{
string s="123456";
int n;
n=converFromString(s);
cout<<n<<endl;
printf("%d\n",n);

int testNum=789;
string ss;
ss=converToString(testNum);
cout<<ss<<endl;
printf(ss.c_str());
return 0;
}
string的操作如下图,大家可以查看相关的帮助手册



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ acm STL 算法 string