c ++查找字符串
Program 1:
程序1:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "";
str.push_back('i');
str.push_back('n');
str.push_back('c');
str.push_back('l');
str.push_back('u');
str.push_back('d');
str.push_back('e');
str.push_back('h');
str.push_back('e');
str.push_back('l');
str.push_back('p');
for (int i = 0; i < str.length(); i++) {
str.pop_back();
}
return 0;
}
Output:
输出:
Varies compiler to compiler.
Explanation:
说明:
The pop_back() is introduced in C++11 for string. And in the earlier versions of C++, it will generate the following error:
在C ++ 11中为字符串引入了pop_back() 。 在早期版本的C ++中,它将生成以下错误:
[Error] 'std::string' has no member named 'pop_back'
Program 2:
程式2:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "";
str.push_back('i');
str.push_back('n');
str.push_back('c');
str.push_back('l');
str.push_back('u');
str.push_back('d');
str.push_back('e');
str.push_back('h');
str.push_back('e');
str.push_back('l');
str.push_back('p');
str.resize(7);
cout << str << endl;
return 0;
}
Output:
输出:
include
Explanation:
说明:
Here, we added string "includehelp" using push_back() function, then we changed the size of the string str using resize() member function and then print string str. So "include" will be printed on the console screen.
在这里,我们使用push_back()函数添加了字符串“ includehelp” ,然后使用resize()成员函数更改了字符串str的 大小 ,然后打印了字符串str 。 因此, “ include”将被打印在控制台屏幕上。
Program 3:
程式3:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string STR = "IncludeHelp";
std::string::iterator ITR;
for (ITR = STR.begin(); ITR != STR.end(); ITR++)
cout << *ITR;
return 0;
}
Output:
输出:
IncludeHelp
Explanation:
说明:
In the above program we created string STR, which is initialized with "IncludeHelp", and we created an iterator ITR. Using iterator ITR we accessed each element of string from beginning to the end and print on the console screen.
在上面的程序中,我们创建了字符串STR ,并使用“ IncludeHelp”对其进行了初始化,并创建了迭代器ITR 。 使用迭代器ITR,我们从头到尾访问了字符串的每个元素,并在控制台屏幕上进行打印。
翻译自: https://www.includehelp.com/cpp-tutorial/strings-find-output-programs-set-5.aspx
c ++查找字符串
本文介绍了C++中关于字符串查找的相关程序,包括使用`pop_back()`、`push_back()`和`resize()`函数操作字符串,以及通过迭代器遍历字符串元素的方法。示例代码展示了不同操作如何影响字符串的输出。

997

被折叠的 条评论
为什么被折叠?



