例题:在链表中查找一个字符串,并插入另一个字符串
2017-03-17 11:40
197 查看
题目为C++ primer 9.28
#include <iostream>
#include <string>
#include <forward_list>
using namespace std;
void Insert(forward_list<string> &lst, const string &a, const string &b);
int main()
{
forward_list<string> lst{ "nayeon","tzuyu","momo","taeyeon" };
string a = "nayeon";
string b = "2yeon";
Insert(lst, a, b);
for (auto it : lst)
cout << it << endl;
system("pause");
return 0;
}
void Insert(forward_list<string> &lst, const string &a, const string &b)
{
auto it = lst.begin();
auto it_begin = lst.before_begin();
while (it != lst.end())
{
if (*it == a)
{
lst.insert_after(it, b);
break;
}
it_begin++;
it++;
}
if (it == lst.end())
{
lst.insert_after(it_begin, b);
}
}
#include <iostream>
#include <string>
#include <forward_list>
using namespace std;
void Insert(forward_list<string> &lst, const string &a, const string &b);
int main()
{
forward_list<string> lst{ "nayeon","tzuyu","momo","taeyeon" };
string a = "nayeon";
string b = "2yeon";
Insert(lst, a, b);
for (auto it : lst)
cout << it << endl;
system("pause");
return 0;
}
void Insert(forward_list<string> &lst, const string &a, const string &b)
{
auto it = lst.begin();
auto it_begin = lst.before_begin();
while (it != lst.end())
{
if (*it == a)
{
lst.insert_after(it, b);
break;
}
it_begin++;
it++;
}
if (it == lst.end())
{
lst.insert_after(it_begin, b);
}
}
相关文章推荐
- [笔试题]查找一个字符串在另一个字符串中的连接次序
- 中文的在一个字符串中查找另一个字符
- PHP:将一个字符创插入到另一个字符串的指定位置
- 查找一个字符串在另一个字符串出现的次数源码
- 将一个字符串插入到另一个字符串的某个位置
- 一个字符串中查找另一个字符串出现的次数
- 经典面试题:求数组的最大子序列和;实现函数 :判断一个自负喜欢是否是另一个字符串旋转所得;杨氏矩阵中查找一个数。
- 将一个字符串插入到另一个字符串的某个位置
- 第十五周 OJ 将一个字符串插入另一个字符串的指定位置
- 关于动态存储分配函数的调用,在已经过排序的数组中查找及删除内容的操作,余数的分析,删除字符数组中的空格,对链表的逆置,在源字符串中查找子字符串的个数,函数指针以及函数的调用,循环赋值带来的问题以及插入
- 查找一个字符串在另一个字符串的位置原理分析
- java实现查找一个字符串在另一个字符串出现的次数
- java在一个字符串前面插入另一个字符串的方法
- c# 查找一个字符串在另一个字符串出现的次数
- 一个字符串插入到另一个字符串
- 查找一个字符串在另一个字符串中出现的所有位置
- 在一个字符串中查找另一个字符串出现的次数
- 一个字符串中查找另一个字符出现的全部索引位置(JAVA)
- 写一个函数,从一个字符串查找另一个字符串的位置
- kmp算法 查找一个字符串在另一个字符串出现的位置