用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重
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 <string>
#include <iostream>
using namespace std;
int main(){
string strinfo="Please input your name:";
cout << strinfo ;
cin >> strinfo;
if( strinfo == "winter" )
cout << "you are winter!"<<endl;
else if( strinfo != "wende" )
cout << "you are not wende!"<<endl;
else if( strinfo < "winter")
cout << "your name should be ahead of winter"<<endl;
else
cout << "your name should be after of winter"<<endl;
strinfo += " , Welcome to China!";
cout << strinfo<<endl;
cout <<"Your name is :"<<endl;
string strtmp = "How are you? " + strinfo;
for(int i = 0 ; i < strtmp.size(); i ++)
cout<<strtmp[i];
return 0;
}
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 <string>
#include <iostream>
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"<<endl;
return -1;
}
int last = strinfo.find_last_of(strset);
if(last == string::npos) {
cout<<"not find any characters"<<endl;
return -1;
}
cout << strinfo.substr(first, last - first + 1)<<endl;//string.substr是子串
return 0;
}
6、insert函数, replace函数和erase函数
string只是提供了按照位置和区间的replace函数,而不能用一个string字串来替换指定string中的另一个字串。
例子:
#include <string>
#include <iostream>
using namespace std;
int main() {
string strinfo="This is Winter, Winter is a programmer. Do you know Winter?";
cout<<"Orign string is :\n"<<strinfo<<endl;
string_replace(strinfo, "Winter", "wende");
cout<<"After replace Winter with wende, the string is :\n"<<strinfo<<endl;
return 0;
}
string.erase(pos,srclen);//srclen是删除的长度
string.insert(pos,strdst); //pos是定位,strdst是插入的函数
void string_replace(string & strBig, const string & strsrc, const string &strdst) {
string::size_type pos=0;
string::size_type srclen=strsrc.size();
string::size_type dstlen=strdst.size();
while( (pos=strBig.find(strsrc, pos)) != string::npos){
strBig.erase(pos, srclen);
strBig.insert(pos, strdst);
pos += dstlen;
}
}
相关链接:http://www.stlchina.org/twiki/bin/view.pl/Main/STLDetailString
7、切割字符串
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string text = "big|dog|china|sonic|free";
stringstream ss(text);
string sub_str;
while(getline(ss,sub_str,'|')) //以|为间隔分割test的内容
cout << sub_str << endl;
return 0;
}
输出如下:
big
dog
china
sonic
free
8、构造函数和析构函数
string s 生成一个空字符串S
string s(str) Copy构造函数,生成字符串Str的一个复制品
string s(str,stridx) 将字符串Str内始于位置Stridx的部分,当作字符串S的初值
string s(str,stridx,strlen) 将字符串Str内始于位置Stridx且长度为strlen的部分,当作字符串S的初值
string s(cstr) 以C-String cstr作为S的初值
string s(num,c) 生成一个字符串,包含Num个C字符
string s(beg,end) 以区间[beg,end]内的字符作为s初值
s.~string() 销毁所有字符,释放内存
注意:
std::string s('x');//error
std::string s(1,'x'); //ok,create a string that has one charactor 'x'
9、substr(string.substr的方法)
(1)参数
start:Number -- 一个整数,指示 my_str 中用于创建子字符串的第一个字符的位置。如果 start 为一个负数,则起始位置从字符串的结尾开始确定,其中 -1 表示最后一个字符。
length:Number -- 要创建的子字符串中的字符数。如果没有指定 length,则子字符串包括从字符串开头到字符串结尾的所有字符。
(2)返回
String -- 指定字符串的子字符串。
(3)示例
下面的示例创建一个新字符串 my_str,并使用 substr() 返回该字符串中的第二个单词;首先,使用正的 start 参数,然后使用负的 start 参数:
var my_str:String = new String("Hello world");
var mySubstring:String = new String();
mySubstring = my_str.substr(6,5);
trace(mySubstring); // 输出:world
mySubstring = my_str.substr(-5,5);
trace(mySubstring); // 输出:world
摘自:http://blog.chinaunix.net/u1/38407/showart_303412.html
分享到:
相关推荐
- 要在使用`CString`的代码中使用`StdString`,可以使用`CString::GetBuffer()`和`new`来创建`StdString`,反之亦然,可以使用`std::wstring_convert`或者`std::wstring`作为中间转换。 示例源码可能包含以下内容...
1. **封装基础操作**:开发者可能会将常用的`std::string`操作如`append`(拼接)、`find`(查找子串)、`replace`(替换子串)等封装为类的方法,以便在代码中更方便地调用。 2. **内存管理**:`std::string`内部...
下面,我们将深入探讨`std::string`类的主要特性和常用操作。 1. **构造与初始化**: - `std::string`可以无参数构造,创建一个空字符串。 - 通过字符数组或另一个`std::string`对象进行初始化。 - 使用`std::...
8. **运算符重载**:`std::string`支持常用的字符串操作运算符,如`+`用于连接字符串,`+=`用于追加,以及比较运算符`==`、`!=`、`、`>`等。 9. **算法操作**:可以使用`std::sort`、`std::reverse`等STL算法对字符...
本文将详细解析两种常用的C++ `std::string`截取字符串的方法:`find`和`find_last_of`,以及如何结合使用它们来满足各种字符串处理需求。 1. `find`方法: `find`方法用于在字符串中查找指定子字符串`strSub`的第...
通常推荐尽可能使用 `std::string` 而不是 C 风格字符串,以避免潜在的内存管理和安全性问题。 #### 五、总结 本文详细介绍了 `std::string` 类的构造方法及其主要的操作函数。通过这些函数,我们可以轻松地实现...
下面将详细介绍 `std::string` 的一些关键特性及其常用方法。 ##### 1.1 string 类型定义 `std::string` 类型由 `<string>` 头文件定义,可以用来表示一系列字符,并且提供了各种操作字符串的方法。`std::string` ...
8. **格式化输出**:在`iostream`中,我们可以使用`运算符将`std::string`对象输出到流中,如`std::cout 。 9. **迭代器**:`std::string`提供了迭代器接口,允许我们使用迭代器遍历字符串中的每个字符。 10. **...
C++中std::string是日常Coding中经常使用的一个类,使用起来非常方便,但是也存在一些弊端。 如下代码,参数传递的过程发生了内存分配(Memory Allocation)和内存拷贝。 void fun(const std::string& s) { std::...
本文将详细介绍`string`的基本使用方法及其常用的操作函数。 ##### 1.1 `string`类型的使用 `string`类型是基于`basic_string`模板类定义的特例化,具体来说就是`basic_string<char>`,即一个字符型的字符串。`...
#### 一、C++ 的 `std::string` 使用 ##### 1.1 C++ `std::string` 简介 在C++标准库中,`std::string` 类是用于处理文本数据的强大工具。它提供了比传统C风格字符串(`char *`)更多的功能和安全性。`std::string` ...
C++的`std::string`类对许多常用的运算符进行了重载,如`+`(拼接)、`=`(赋值)、`+=`(追加并赋值)、`[]`(下标访问)、`(用于输出流)等。这使得操作字符串更加直观和便捷。 ```cpp std::string str1 = ...
在本篇文章中,我们将深入探讨`std::string`的使用,涵盖其成员函数、构造、重载运算符以及与其他库如Boost和C字符串的结合使用。 1. C++ string 简介 C++的`std::string`是一个类模板,提供了动态可变长度的字符...
下面将详细介绍 `string` 类的一些常用成员函数: 1. **`append`**: 将一个字符串追加到另一个字符串后面。 - **语法:** `void append(const string& str);` - **示例:** `std::string s1 = "Hello"; s1.append...
本教程将深入探讨`std::string`类的常用功能,并通过实例讲解其用法。 1. **创建和初始化字符串** - `std::string`对象可以空初始化,如`std::string str;` - 也可以直接赋值初始化,如`std::string str = "Hello...
6. **预编译头文件**:为了提高编译速度,可以使用预编译头文件(如`#include <android/ndk.h>`)来包含常用头文件。 7. **编译标志**:在构建C++代码时,可能需要添加特定的编译标志,如`-llog`链接日志库,`-std=...
- **使用 `substr()` 方法**:`std::string sub = str.substr(pos, len);` 获取从位置 `pos` 开始长度为 `len` 的子串。 - **使用迭代器**:`string::iterator it = str.begin();` 和 `string::iterator it = str....
本篇文章将深入探讨`std::string`类的一些常用方法,帮助你更好地理解和运用这个强大的类型。 1. **构造与初始化** - `std::string()`:默认构造函数,创建一个空字符串。 - `std::string(const char*)`:通过...
### C++ STL String 常用函数详解 在C++标准模板库(STL)中,`std::string` 类提供了一系列强大的字符串处理功能。本文将详细介绍`std::string`类的一些核心成员函数及其应用场景。 #### 构造与赋值 1. **构造函数*...