9.4
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<list>
#include<deque>
using namespace std;
bool findInt(vector<int>& vec, int x)
{
for (auto i: vec)
{
if (x == i)
{
return true;
}
}
return false;
}
int main(int argc , char ** argv)
{
vector<int> vec = {4 , 5, 6, 8 , 7 , 9};
int needfind = 5;
if (findInt(vec ,needfind ))
{
cout << "we find that number : " << needfind << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
return 0;
}
9.5
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<list>
#include<deque>
using namespace std;
vector<int>::iterator findInt(vector<int>::iterator a,vector<int>::iterator b , int x)
{
for (a; a != b ; ++a)
{
if (x == *a)
{
return a ;
}
}
return b;
}
int main(int argc , char ** argv)
{
vector<int> vec = {4 , 5, 6, 8 , 7 , 9};
int needfind = 5;
if (findInt(vec.begin() , vec.end() , needfind ) != vec.end())
{
cout << "that number : " << needfind << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
return 0;
}
9.28
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
using namespace std;
void func(forward_list<string>& a, string b, string c)
{
auto it = a.begin();
auto prev = a.before_begin();
int cnt = 0;
while (it != a.end())
{
if (*it == b)
{
it = a.insert_after(it,c);
cnt++;
break;
}
else
{
prev = it;
++it;
}
}
if (cnt == 0)
{
a.insert_after(prev, c);
}
}
int main(int argc , char ** argv)
{
forward_list<string> list(4, "sad");
string a = "sad", b = "sad2";
func(list, a, b);
for (auto i : list)
{
cout << i << " ";
}
cin.get();
}