在C++中,我们可以通过以下两种方式之一存储字符串:
- C风格的字符串
- 字符串类(在本文中讨论)
在本文中,将讨论第二种方法。字符串类是C++库的一部分,它比C样式字符串支持很多功能。
C++字符串类在内部使用char数组存储字符,但是所有的内存管理,分配和null终止都由字符串类本身处理,这就是为什么它易于使用。由于类似于矢量的动态内存分配,因此可以在运行时更改C++字符串的长度。由于字符串类是容器类,因此我们可以使用类似于矢量,集合和映射之类的其他容器的迭代器来迭代其所有字符,但通常,我们使用简单的for循环来迭代字符并使用[]运算符对其进行索引。
C++字符串类具有许多轻松处理字符串的功能。下面的代码演示了它们中最有用的。
// C++ program to demonstrate various function string class
#include
using namespace std;
int main()
{
// various constructor of string class
// initialization by raw string
string str1("first string");
// initialization by another string
string str2(str1);
// initialization by character with number of occurrence
string str3(5, '#');
// initialization by part of another string
string str4(str1, 6, 6); // from 6th index (second parameter)
// 6 characters (third parameter)
// initialization by part of another string : iteartor version
string str5(str2.begin(), str2.begin() + 5);
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
// assignment operator
string str6 = str4;
// clear function deletes all character from string
str4.clear();
// both size() and length() return length of string and
// they work as synonyms
int len = str6.length(); // Same as "len = str6.size();"
cout << "Length of string is : " << len << endl;
// a particular character can be accessed using at /
// [] operator
char ch = str6.at(2); // Same as "ch = str6[2];"
cout << "third character of string is : " << ch << endl;
// front return first character and back returns last character
// of string
char ch_f = str6.front(); // Same as "ch_f = str6[0];"
char ch_b = str6.back(); // Same as below
// "ch_b = str6[str6.length() - 1];"
cout << "First char is : " << ch_f << ", Last char is : "
<< ch_b << endl;
// c_str returns null terminated char array version of string
const char* charstr = str6.c_str();
printf("%s\n", charstr);
// append add the argument string at the end
str6.append(" extension");
// same as str6 += " extension"
// another version of append, which appends part of other
// string
str4.append(str6, 0, 6); // at 0th position 6 character
cout << str6 << endl;
cout << str4 << endl;
// find returns index where pattern is found.
// If pattern is not there it returns predefined
// constant npos whose value is -1
if (str6.find(str4) != string::npos)
cout << "str4 found in str6 at " << str6.find(str4)
<< " pos" << endl;
else
cout << "str4 not found in str6" << endl;
// substr(a, b) function returns a substring of b length
// starting from index a
cout << str6.substr(7, 3) << endl;
// if second argument is not passed, string till end is
// taken as substring
cout << str6.substr(7) << endl;
// erase(a, b) deletes b characters at index a
str6.erase(7, 4);
cout << str6 << endl;
// iterator version of erase
str6.erase(str6.begin() + 5, str6.end() - 3);
cout << str6 << endl;
str6 = "This is a examples";
// replace(a, b, str) replaces b characters from a index by str
str6.replace(2, 7, "ese are test");
cout << str6 << endl;
return 0;
}
输出 :
first string
first string
#####
string
first
Length of string is : 6
third character of string is : r
First char is : s, Last char is : g
string
string extension
string
str4 found in str6 at 0 pos
ext
extension
string nsion
strinion
These are test examples
如上面的代码所示,我们可以通过size()以及length()来获取字符串的长度,但是length()是字符串的首选。我们可以通过+ =或append()将一个字符串到另一个字符串,但是+ =的速度比append()稍慢,因为每次调用+都会创建一个新字符串(创建新缓冲区),并返回该字符串为许多追加操作时的位开销。
应用范围:
基于上述字符串函数,一些应用程序如下所示:
// C++ program to demonstrate uses of some string function
#include
using namespace std;
// this function returns floating point part of a number-string
string returnFloatingPart(string str)
{
int pos = str.find(".");
if (pos == string::npos)
return "";
else
return str.substr(pos + 1);
}
// This function checks whether a string contains all digit or not
bool containsOnlyDigit(string str)
{
int l = str.length();
for (int i = 0; i < l; i++)
{
if (str.at(i) < '0' || str.at(i) > '9')
return false;
}
// if we reach here all character are digits
return true;
}
// this function replaces all single space by %20
// Used in URLS
string replaceBlankWith20(string str)
{
string replaceby = "%20";
int n = 0;
// loop till all space are replaced
while ((n = str.find(" ", n)) != string::npos )
{
str.replace(n, 1, replaceby);
n += replaceby.length();
}
return str;
}
// driver function to check above methods
int main()
{
string fnum = "23.342";
cout << "Floating part is : " << returnFloatingPart(fnum)
<< endl;
string num = "3452";
if (containsOnlyDigit(num))
cout << "string contains only digit" << endl;
string urlex = "google com in";
cout << replaceBlankWith20(urlex) << endl;
return 0;
}
输出 :
Floating part is : 342
string contains only digit
google%20com%20in
相关文章:
- 如何在C++中快速反转字符串?
- C++字符串类及其应用程序套装2
- C++中的字符串数组
- 在C++中将字符串转换为数字,反之亦然
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。