int main()
{
vector<int> vi = { 1,2,3,4,5,6,7,8 ,9 };
auto iter = vi.begin();
string tmp;
while (iter != vi.end()) {
if (*iter % 2) {
iter = vi.insert(iter, *iter);
++iter;
}
++iter;
for (auto begin = vi.begin(); begin != vi.end(); begin++)
cout << *begin << " ";
cout << endl;
cin >> tmp;
}
return 0;
}
void replace_string(string &s, const string &oldVal, const string &newVal)
{
auto p = s.npos;
while ((p = s.find(oldVal)) != s.npos) {
s.replace(p, oldVal.size(), newVal);
p += newVal.size();
}
}
void rep_string(string &s, const string &old, const string &neww)
{
auto iter = s.begin();
while (iter != s.end()) {
auto iter1 = iter;
auto iter2 = old.begin();
while ((iter2 != old.end()) && (*iter2 == *iter1)) {
iter2++;
iter1++;
}
if (iter2 == old.end()) {
s.erase(iter - s.begin(), old.size());
s.insert(iter - s.begin(), neww);
iter += neww.size();
}
else
iter++;
}
}
int main()
{
string s;
string newval, oldval;
cout << "请输入一个字符串:" << endl;
getline(cin, s);
cout << "请输入要替换的元素:" << endl;
cin >> oldval;
cout << "将元素替换为:" << endl;
cin >> newval;
cout << "原来的s:" << s << endl;
replace_string(s, oldval, newval);
cout << "改变后的s:" << s << endl;
}
int main()
{
string::size_type pos = 0;
string num{ "0123456789" };
string s1{ "ab2c3d7r4e6" };
while ((pos = s1.find_first_of(num, pos)) != string::npos) {
cout << s1[pos];
pos++;
}
pos = 0;
while ((pos = s1.find_first_not_of(num, pos)) != string::npos) {
cout << s1[pos];
pos++;
}
}
int main(int argc, char *argv[])
{
string s1{ "bdfghjklpqty" }, word, maxword;
string::size_type maxsize = 0;
ifstream in(argv[1]);
if (!in) {
cerr << "未能打开文件!" << endl;
return -1;
}
cout << "文件内容为:" << endl;
while (in >> word) {
cout << word << " ";
if (word.find_first_of(s1) != word.npos)
continue;
if (word.size() > maxsize) {
maxsize = word.size();
maxword = word;
}
}
cout << endl;
cout << "最长的词为:" << maxword << endl;
}
string name_string(string &name, string &prefix, const string &sufix)
{
name.insert(name.begin(), ' ');
name = prefix.append(name);
name.append(" ");
name.append(sufix);
return name;
}
int main()
{
string name, prefix, sufix;
cout << "输入名字" << endl;
cin >> name;
cout << "输入前缀" << endl;
cin >> prefix;
cout << "输入后缀" << endl;
cin >> sufix;
name = name_string(name, prefix, sufix);
cout << "合成结果为:" << name << endl;
}