标准c++中string类介绍
string str;
把char*(c类型的string)数组转换为string:
str = ch; //即可
str.assign(ch); //也可
把string类型转换为char*数组:
strcpy(ch,str.c_str()); //即可
for(int i = 0; i < str.length(); i++) //也可
{
ch = str.at(i);
ch ++;
}
比较两个字符串相等
string str1;
string str2;
strcmp(str1.c_str(),str2.c_str());
< 0 string1 less than string2
= 0 string1 identical to string2
> 0 string1 greater than string2
清空一个char*数组
memset(void *dest,int c,size_t count)
拷贝char*数组
memcpy(void *dest,const void * src,size_t count)
判断是否是一个字符串的子串
strstr();
str.find("")
1. 创建并初始化字符串
======================================================================================
string s;
string s(“string”);
string s = str;
string s(str,stridx,strlen);
string s(chars,chars_len);
string s(num,c);
string s(s1.begin(),s1.end()); 采用迭代器方法,选取字符串区间beg-end(不包含end)内的字符作为字符串s的初值
string s = “a”+”b”;
string s = str.substr(stridx,strlen);
使string对象的用成员函数substr()来初始化字符串,其中stridx、strlen的默认值分别为0、极大整型值
======================================================================================
实例1:创建并初始化字符串
======================================================================================
#include <string>
#include <iostream>
using namespace std;
int main() {
} ///:~
======================================================================================
======================================================================================
s.insert(stridx, str );
s.append(str);
a = a + str;
===============================================================================
实例2:插入、追加和连接操作
======================================================================================
#include <string>
#include <iostream>
using namespace std;
int main() {
} ///:~
===============================================================================
===============================================================================
s.erase(stridx, strlen);
======================================================================================
======================================================================================
s.replace(stridx, strlen, str); 对指定下标开始到指定长度strlen区间的字符序列用字符 串str替换
======================================================================================
======================================================================================
s.find(char|str, stridx);
s. find_first_of(char|str, stridx);
s. find_last_of(char|str, stridx);
s. find_first_not_of(char|str, stridx);
s. find_last_not_of(char|str, stridx);
s.rfind(char|str, stridx);
======================================================================================
实例3:字符串的查找(查操作)
======================================================================================
#ifndef SIEVE_H
#define SIEVE_H
#include <cmath>
#include <cstddef>
#include <string>
#include "../TestSuite/Test.h"
using std::size_t;
using std::sqrt;
using std::string;
class SieveTest : public TestSuite::Test {
public:
};
#endif // SIEVE_H ///:~
#include "Sieve.h"
int main() {
} ///:~
======================================================================================
实例4:
======================================================================================
#ifndef COMPSTR_H
#define COMPSTR_H
#include <string>
#include "../TestSuite/Test.h"
using std::string;
class CompStrTest : public TestSuite::Test {
public:
};
#endif
#include "CompStr.h"
int main() {
} ///:~
======================================================================================
-
两个完整的字符串: str1.compare(str2);
-
一个字符串的某一部分和另一个字符串的全部: str1.compare(stridx, strlen, str2);
-
两个字符串的子串: str1.compare(stridx, strlen, str2, stridx, strlen);
其中,stridx是给定字符串进行比较部分的开始,stdlen是要比较的字符长度。
实例: compare函数
======================================================================================
#include <cassert>
#include <string>
using namespace std;
int main() {
} ///:~
======================================================================================
除了以上介绍到的对字符串的操作,下面对于一些经常用到的几个有意义的函数做如下解释及对比:
======================================================================================
s.capacity();
s.reserve(strlen);
s.resize(strlen);
s.swap( str);
s.clear();
s[stridx];
s.at(stridx);
s.empty();
======================================================