string::iterator i;i = str.begin(); //string类型的迭代器 str.erase(str.begin()+2,str.end()-1);cout<<str<<"\n";} inpot 1234567890 output 120 int i = 3; str.erase(i,3);input 1234567890 ouput 1237890 int i = 3;str.erase(i,1);input 12345678 output 1235678 int i...
2、string构造函数 void test01() { string s1;//默认构造 const char* str = "hello world"; string s2(str); cout << "s2=" << s2 << endl; string s3(s2); cout << "s3=" << s3 << endl; string s4(10, 'a');//10个a cout << "s4=" << s4 << endl; } 3、string赋值...
vector<string>::iterator t = v.begin();//相当于取数组第一个元素 v.erase(t);//删除
#include <iostream>#include <string>#include <algorithm>#include <cctype>intmain() { std::string a ="!!eix!@#$%fghjk^&*()_+%if***"; std::cout << a <<'\n'; a.erase( std::remove_if( a.begin(), a.end(), [](charc ) {returnstd::ispunct(c); } ), a.end() ) ; ...
在overflow无意中看到一个提问: 删除字符串中所有的'a'. https://stackoverflow.com/questions/20326356/how-to-remove-all-the-occurrences-of-a-char-in-c-string底下有个回答: 当时就有点懵逼,为何可…
、方法1 通过一个string对象来将元素不断加入其中,加完以后找出空格,删除即可 内容介绍1 string转char*的方法c_str(),返回一个指向正规C字符串的指针,内容与本string串相同...(position);删除position处的一个字符(position是个string类型的迭代器)(3)erase(first,last);删除从first到last之间的字符(first和last...
#include <iostream> #include using namespace std; int main(void) { /* Multimap with duplicates */ multimap<char, int> m { {'a', 1}, {'a', 2}, {'b', 3}, {'c', 4}, {'c', 5}, }; cout << "Multimap contains following elements before erase operation" << endl; for (...
Let us compile and run the above program, this will produce the following result − Unordered map contains following elements before erase operation e = 5 a = 1 b = 2 c = 3 d = 4 Unordered map contains following elements after erase operation a = 1 b = 2 c = 3 d = 4 ...
<string_view> #include <vector> void print_container(std::string_view comment, const std::vector<char>& c) { std::cout << comment << "{ "; for (auto x : c) std::cout << x << ' '; std::cout << "}\n"; } int main() { std::vector<char> cnt(10); std::iota(cnt....
(cnt, '3'); std::cout << "Erase \'3\':\n"; print_container(cnt); std::erase_if(cnt, [](char x) { return (x - '0') % 2 == 0; }); std::cout << "Erase all even numbers:\n"; print_container(cnt); std::cout << "In all " << erased << " even numbers were ...