写在前面:有些题没有测试 可能有些错误 刚接触C++ 莫怪 共同探讨
11 9 8 7 6 5
头文件
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
15章 11题 将链表拷贝到向量(vector)中 验证 排序 并打印
bool ascending_order(double a, double b){
return a<b;
}
void myPrint(double value){
cout<<value<<" ";
}
void copy_operation(list<int>&L, vector<double> V){
for(list<int>::iterator il = L.begin(); il != L.end(); il++){
V.push_back(*il);
}
}
bool inspect_operation(list<int>&L, vector<double> V){
list<int>::iterator il = L.begin();
vector<double>::iterator iv = V.begin();
for(; il != L.end() && iv != V.end() ; il++, iv++){
if( *il != (int)*iv ){
return false;
}
}
if( il != L.end() || iv != V.end() ){
return false;
}
return true;
}
void test01(){
vector<double> v;
v.push_back(5);
v.push_back(6);
v.push_back(2);
v.push_back(1);
v.push_back(3);
v.push_back(4);
sort(v.begin(), v.end(), ascending_order);
for_each(v.begin(), v.end(), myPrint);
}
15章 9题 统计Document中单词总数 1 以空白符分割 2以不是字母的字符分割
int CountWord(list<vector<char>>&L){
int choice = 0;
cout<<"请选择以那种方式分割:";
cin>>choice;
int num = 0;
for(list<vector<char>>::iterator it = L.begin(); it != L.end(); it++){
for(vector<char>::iterator in = (*it).begin(); in != (*it).end(); in++){
if((int)*in == 32 || choice == 1){//书中以 空格 为字符的间隔 空格 的ASCII值为 32
num++;
}
if(choice == 2){
if( (int)*in <= 65 || ( (int)*in >= 90 && (int)*in <= 97 ) || (int)*in >= 122){//不在字母范围内的ASCII码值
num++;
}
}
}
}
return num;
}
15章 8题 编写一个可统计Document中字符总数的函数
int CountCharacter(list<vector<char>>&L){
int num = 0;
for(list<vector<char>>::iterator it = L.begin(); it != L.end(); it++){
for(vector<char>::iterator in = (*it).begin(); in != (*it).end(); in++){
if((int)*in != 32){//书中以 空格 为字符的间隔 空格 的ASCII值为 32
num++;
}
}
}
return num;
}
15章 7题在未排序的vector中查找按字典顺序排在最后面的字符串
string find_lastString(vector<string> &v){
string LastString = *(v.begin());
for(vector<string>::iterator it = v.begin(); it != v.end(); it++){
if(LastString[0]< (*it)[0]){
LastString = *it;
}
}
return LastString;
}
void test01(){
vector<string>v;
v.push_back("aBook");
v.push_back("vLast");
v.push_back("dVois");
v.push_back("String");
v.push_back("zVector");
v.push_back("cPush");
v.push_back("aBack");
string LastString = find_lastString(v);
cout<<LastString<<endl;
}
15章 6题 为Document编写一个 查找并替换 操作
bool find_replace(char f, char r){
for(list<vector<char>::iterator in = begin().ln; it != end(); it++){
for(vector<char>::iterator Pos = in.pos; Pos != (*in).end(); Pos++){
if(*Pos == f){
*Pos = r;
return true;//找到了返回true 替换成功
}
}
}
return false;
}
15章 5题 为vector定义输入和输出运算符
template<typename T>
class ioVector{
public:
ioVector(int capacity = 0){
this->elem = new T[capacity];
this->size = 0;
}
T * elem;
int size;
};
template <typename T>
ostream& operator<<(ostream &cout,ioVector<T> &v){
for(int i=0; i<v.size; i++){
cout<<v.elem[i]<<" ";
}
return cout;
}
template <typename T>
istream & operator>>(istream &cin ,ioVector<T>&v){//若需要调用私有成员 在类中声明成友元即可
cin>>v.elem[v.size];
v.size++;
return cin;
}
void test01(){
ioVector<int> v(2);
cout<<"请输入两个数字 以空格间隔"<<endl;
cin>>v>>v;
cout<<v<<" ";
}
测试
int main(){
//test01();
}