cs11_adventure c++_lab1

时间:2023-03-09 03:42:52
cs11_adventure c++_lab1

exercise1.cc

 #include <iostream>
#include <vector>
#include <stdlib.h>
#include <algorithm> using namespace std; int myFunction()
{
return rand() % + ;
} int main()
{
vector<int> vi1();
generate(vi1.begin(), vi1.end(), myFunction);//可用generate函数初始化,与下功能一致
/*
for(int i = 0; i < 100; i++)
{
vi1[i] = (rand() % 100 + 1);
}
*/
vector<int> vi2(vi1.size());
copy(vi1.begin(), vi1.end(), vi2.begin()); for(int i = ; i < ; i++)
cout << i << " "<< vi1[i] << " " << vi2[i] << endl;
}

exercise2.cc

 #include <iostream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <string> using namespace std; string randomString()
{
int i = rand() % + ;//随机字符串长度
string str = "";
for(int j = ; j < i; j++)
{
str += 'a' + rand() % ;//随机字符串中内容
}
str += '\0';//字符串末尾处理
return str;
} bool myFunction(string s1, string s2)//自定义谓词1
{
return s1.size() < s2.size();
} struct myClass//自定义谓词2
{
bool operator()(string s1, string s2)
{
return s1.size() < s2.size();
}
}myObject; /*
class myClass//自定义谓词2
{
public:
bool operator()(string s1, string s2)
{
return s1.size() < s2.size();
}
};
myClass myObject;
*/ int main()
{
vector<string> vs();
vector<string>::iterator vsi; for(vsi = vs.begin(); vsi != vs.end(); vsi++)//填充随机字符串
{
*vsi = randomString();
} for(int j = ; j < ; j++)//输出原始随机字符串
{
cout << j << " " << vs[j] << endl;
}
cout << "原始值----------------------------" << endl; sort(vs.begin(), vs.end());//默认排序并输出
for(int j = ; j < ; j++)
{
cout << j << " " << vs[j] << endl;
}
cout << "第一次字母序排序------------------------"<< endl; sort(vs.begin(), vs.end(), myFunction);//自定义谓词1排序并输出
sort(vs.begin(), vs.end(), myObject);//自定义谓词2排序并输出
for(int j = ; j < ; j++)
{
cout << j << " " << vs[j] << endl;
}
cout << "第二次按长度排序------------------------"<< endl;
}

exercise3.cc

 #include <iostream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <string> using namespace std; int randomNum()
{
return rand() % + ;
} struct myClass//自定义谓词
{
int even;
int odd;
bool operator()(int num)
{
if(num % )
{
even++ ;
return true;
}
else
{
odd++;
return false;
}
}
}myObject; int main()
{
vector<int> vi();
vector<int>::iterator itor; for(itor = vi.begin(); itor != vi.end(); itor++)//填充随机字符串
{
*itor = randomNum();
} for(int i = ; i < ; i++)//输出原始随机字符串
{
cout << i << " " << vi[i] << endl;
}
cout << "原始值----------------------------" << endl; myClass result = for_each(vi.begin(), vi.end(), myClass());//第一种调用方式,带状态
myClass result = for_each(vi.begin(), vi.end(), myObject);//第二种调用方式,但是,其中odd和even是如何初始化为零的?
cout << result.odd << endl << result.even << endl;
}

exercise4.cc

 #include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <ext/functional>//为支持compose1函数额外添加的头文件 using namespace std; int main()
{
//初始化vector
vector<int> v;
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back(); //把vector中内容通过指定的流迭代器写入到指定流中,放一个整数,后跟一个“ ”
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl; //remove_if移除序列中谓词返回true的元素,但是容器长度不变,所有元素还在容器中,其实是把所有应移除元素至于容器尾部并返回一个分界迭代器
//compose1(f,g)函数执行顺序是f(g),先执行g,把g的结果作为f的参数
//bind2nd函数是把一个二元谓词转化成一元谓词的函数,绑定第二个参数,使之成为一个一元谓词
//modulus函数是取模函数,被绑定模2
//那么所有的偶数都被标记,非偶数都被至于前面,返回的是指向8的迭代器
vector<int>::iterator new_end =
remove_if(v.begin(), v.end(),
__gnu_cxx::compose1(bind2nd(equal_to<int>(), ),
bind2nd(modulus<int>(), ))); copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}

exeercise5.cc

 #include <iostream>
#include <vector>
#include <algorithm>
#include <iterator> using namespace std; template <typename BidirectionalIterator>
void my_reverse(BidirectionalIterator first, BidirectionalIterator last)
{
if(distance(first, last) == )//当什么都没有的情况
{
cout<<"为零换个屁啊"<<endl;
return;
}
else
{
while(distance(first, last-) > )//这是考虑除了迭代器重合外的所有情况,只有在两迭代器指向不同位置的时候才会对换,指向同一位值就不对换了
{
cout<<*first<<"---"<<*(last-)<<endl;//对换的结构
swap(*first, *(last-));//注意,不是first和last对换,那是迭代器互相赋值,不对。也不是*first和*last之间的对换,因为last指向的是最后元素之后的元素,也不对。
first++;
last--;
}
}
} int myFunction()
{
return rand() % + ;
} int main()
{
int size;
cout << "input size: " << endl;
cin >> size;
vector<int> vi(size); generate(vi.begin(), vi.end(), myFunction);
copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
cout << endl; my_reverse(vi.begin(), vi.end());
copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}