1. String class clear/ Erase /pop_back
1.1 STD: : string: : clear
prototype: void clear() noexcept; description: clears the contents of the string to make the source string an empty string (0 characters in length). Code examples:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
cout<<"Please enter one line, ending with a newline character."<<endl;
getline(std::cin, str);
cout<<"Before Clearance.str = \""<<str<<"\", str.size = "<<str.size()<<endl;
str.clear();
cout<<"After emptying.str = \""<<str<<"\", str.size = "<<str.size()<<endl;
if(true == str.empty())
{
cout<<"The source string has been cleared."<<endl;
}
system("pause");
return 0;
}
=>Please enter one line, ending with a newline character.
hello world.
Before clearing: str = "hello world.", str.size = 12
Empty: str = "", str.size = 0
The source string has been cleared.
1.2 STD: : string: : erase
archetype: string& erase (size_t pos = 0, size_t len = npos); note: delete the len characters of the source string starting with the subscript pos, and return the modified string. stereotype: iterator erase (const_iterator p); description: deletes the character pointed to by iterator p in the source string, returning the position of the iterator after deletion. stereotype: iterator erase (const_iterator first, const_iterator last); note: delete all characters in the source string iterator range [first, last], returning the position of the iterator after deletion.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("This is an example sentence.");
cout<<str<<endl;
str.erase(10, 8);
cout<<str<<endl;
str.erase(str.begin()+9);
cout<<str<<endl;
str.erase(str.begin()+5, str.end()-9);
cout<<str<<endl;
system("pause");
return 0;
}
=>This is an example sentence.
This is an sentence.
This is a sentence.
This sentence.
1.3 STD: : string: : pop_back
Void pop_back(); void pop_back(); : removes the last character of the source string, effectively reducing its length. Code examples:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("hello world!");
str.pop_back();
cout<<str<<endl;
system("pause");
return 0;
}
=>hello world
[1] network resources:
http://www.cplusplus.com/reference/string/string/clear/
http://www.cplusplus.com/reference/string/string/erase/
http://www.cplusplus.com/reference/string/string/pop_back/
Read More:
- Vector delete pop of element_ back(),erase(),remove()
- Differences between length() size() and C strlen() of C + + string member functions
- C++:error C2228: left of ‘.str’ must have class/struct/union
- C++ string substr()
- How do you set, clear and toggle a single bit in C?
- C language string processing error warning, c4996, sprintf, predicted, c4996, strcpy, c4996, strcat
- The reasons and common solutions of vs flash back on the road of C + + Learning (with reasons attached)
- Solve the problem of error: cannot pass objects of non trivially copyable type ‘STD:: String’ in C / C + +
- The problem of flash back by pressing enter window when debugging or executing program in Visual Studio C
- On the intern () method of string class in Java
- Error c2011: “a certain class”: redefinition of “class” type
- The differences between the equals method in the string class and the equals method in the object class
- C language program running results flash back how to do
- A repeated string is composed of two identical strings. For example, abcabc is a repeated string with length of 6, while abcba does not have a duplicate string. Given any string, please help Xiaoqiang find the longest repeated substring.
- Solution to the problem of console output window’s CMD flashing back when vs2015 writes C + + program
- String operation to delete the character at the specified position
- Mybatis error,There is no getter for property named ‘xx’ in ‘class java.lang.String The solution
- Error c2259 cannot instance abstract class due to following members
- Codeworks educational round 96 [reverse pair] E. string reverse
- TSLint:object access via string literals is disallowedtslint(no-string-literal)