STL库相关练习代码
第一题:
#include <iostream>
#include <vector>
#include <iterator>
#include <string> using namespace std; bool huiwen(string input); int main()
{
string input;
cout<<"请输入你的字符串\n"<<endl;
getline(cin,input);
if(huiwen(input))
{
cout<<"是回文\n";
}
else
{
cout<<"不是回文\n";
}
} bool huiwen(string input)
{
string::iterator forware=input.begin();
string::reverse_iterator backware=input.rbegin();
int input_length=input.length();
for(int i=0;i<input_length/2;i++)
{
if(*forware==*backware)
{
forware++;
backware++;
}
else
{
return false;
}
return true;
}
}
第二题
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <string> using namespace std; bool huiwen(string input); int main()
{
string input;
cout<<"请输入你的字符串\n";
getline(cin,input);
string::iterator it;
for(it=input.begin();it!=input.end();)
{
if(isalpha(*it))
{
it=input.erase(it);
continue;
}
else
{
*it=tolower(*it);
it++;
}
}
if(huiwen(input))
{
cout<<"为回文\n";
}
else
{
cout<<"不是回文\n";
}
} bool huiwen(string input)
{
string::iterator forware=input.begin();
string::reverse_iterator backware=input.rbegin();
int input_length=input.length();
for(int i=0;i<input_length/2;i++)
{
if(*forware==*backware)
{
forware++;
backware++;
}
else
{
return false;
}
return true;
}
}
第三题
#include <iostream>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <iterator>
#include <vector>
#include <fstream>
#include <ctime>
#include <list>
#include <queue>
#include <memory>
using std::string;
using namespace std;
const string Filename="word.txt"; int main()
{
using std::cout;
using std::cin;
using std::tolower;
using std::endl;
vector<string> wordlist;
ifstream in;
in.open(Filename.c_str());
string inword; while(in>>inword)
{
wordlist.push_back(inword);
} std::srand(std::time(0));
char play;
cout << "Will you play a word game? <y/n> ";
cin >> play;
play = tolower(play);
while (play == 'y')
{
string target = wordlist[std::rand() % wordlist.size()];
int length = target.length();
string attempt(length, '-');
string badchars;
int guesses = 6;
cout << "Guess my secret word. It has " << length
<< " letters, and you guess\n"
<< "one letter at a time. You get " << guesses
<< " wrong guesses.\n";
cout << "Your word: " << attempt << endl;
while (guesses > 0 && attempt != target)
{
char letter;
cout << "Guess a letter: ";
cin >> letter;
if (badchars.find(letter) != string::npos
|| attempt.find(letter) != string::npos)
{
cout << "You already guessed that. Try again.\n";
continue;
}
int loc = target.find(letter);
if (loc == string::npos)
{
cout << "Oh, bad guess!\n";
--guesses;
badchars += letter; // add to string
}
else
{
cout << "Good guess!\n";
attempt[loc]=letter;
// check if letter appears again
loc = target.find(letter, loc + 1);
while (loc != string::npos)
{
attempt[loc]=letter;
loc = target.find(letter, loc + 1);
}
}
cout << "Your word: " << attempt << endl;
if (attempt != target)
{
if (badchars.length() > 0)
cout << "Bad choices: " << badchars << endl;
cout << guesses << " bad guesses left\n";
}
}
if (guesses > 0)
cout << "That's right!\n";
else
cout << "Sorry, the word is " << target << ".\n";
cout << "Will you play another? <y/n> ";
cin >> play;
play = tolower(play);
} cout << "Bye\n"; return 0;
}
第四题
#include <iostream>
#include <stdlib.h>
#include <list>
#include <algorithm> int reduce(long qr[],int n); using namespace std; int main()
{
long ar[] = { 12, 2, 13, 12, 2, 55, 32, 44, 32, 100, 32, 12 };
int length_old=12;
int length_new;
cout<<"开始输出原始数据\n"<<endl;
for(int i=0;i<length_old;i++)
{
cout<<ar[i]<<' ';
}
cout<<endl;
length_new=reduce(ar,length_old);
cout<<"开始输出新的字符串"<<endl;
for(int i=0;i<length_new;i++)
{
cout<<ar[i]<<' ';
} }
int reduce(long qr[],int n)
{
list<long> la(qr,qr+n);
la.sort();
la.unique(); int len=0;
for(list<long>::iterator it=la.begin();it!=la.end();it++)
{
*(qr+len)=*it;
len++;
}
for(int i=len;i<n;i++)
{
*(qr+len)=0;
}
return len;
}
第六题
#include <iostream>
#include <stdlib.h>
#include <list>
#include <algorithm> using namespace std;
template<class T>
int reduce(T qr[],int n); int main()
{
long ar[] = { 12, 2, 13, 12, 2, 55, 32, 44, 32, 100, 32, 12 };
int length_old=12;
int length_new;
cout<<"开始输出原始数据\n"<<endl;
for(int i=0;i<length_old;i++)
{
cout<<ar[i]<<' ';
}
cout<<endl;
length_new=reduce(ar,length_old);
cout<<"开始输出新的字符串"<<endl;
for(int i=0;i<length_new;i++)
{
cout<<ar[i]<<' ';
} }
template<class T>
int reduce(T qr[],int n)
{
list<T> la(qr,qr+n);
la.sort();
la.unique(); int len=0;
for(typename list<T>::iterator it=la.begin();it!=la.end();it++)
{
*(qr+len)=*it;
len++;
}
for(int i=len;i<n;i++)
{
*(qr+len)=0;
}
return len;
}
第6题
#include <iostream>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
#include <queue> class Customer
{
private:
long arrive; // arrival time for customer
int processtime; // processing time for customer
public:
Customer() { arrive = processtime = 0; }
void set(long when);
long when() const { return arrive; }
int ptime() const { return processtime; }
};
void Customer::set(long when)
{
processtime = std::rand() % 3 + 1;
arrive = when;
} const int MIN_PER_HR = 60; bool newcustomer(double x); // is there a new customer? int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::ios_base;
// setting things up
std::srand(std::time(0)); // random initializing of rand() cout << "Case Study: Bank of Heather Automatic Teller\n";
cout << "Enter maximum size of queue: ";
int qs;
cin >> qs;
std::queue<Customer> line; // line queue holds up to qs people cout << "Enter the number of simulation hours: ";
int hours; // hours of simulation
cin >> hours;
// simulation will run 1 cycle per minute
long cyclelimit = MIN_PER_HR * hours; // # of cycles cout << "Enter the average number of customers per hour: ";
double perhour; // average # of arrival per hour
cin >> perhour;
double min_per_cust; // average time between arrivals
min_per_cust = MIN_PER_HR / perhour; Customer temp; // new customer data
long turnaways = 0; // turned away by full queue
long customers = 0; // joined the queue
long served = 0; // served during the simulation
long sum_line = 0; // cumulative line length
int wait_time = 0; // time until autoteller is free
long line_wait = 0; // cumulative time in line // running the simulation
for (int cycle = 0; cycle < cyclelimit; cycle++)
{
if (newcustomer(min_per_cust)) // have newcomer
{
if (qs==line.size())
turnaways++;
else
{
customers++;
temp.set(cycle); // cycle = time of arrival
line.push(temp); // add newcomer to line
}
}
if (wait_time <= 0 && !line.empty())
{
temp=line.front(); // attend next customer
wait_time = temp.ptime(); // for wait_time minutes
line_wait += cycle - temp.when();
served++;
}
if (wait_time > 0)
wait_time--;
sum_line += line.size();
} // reporting results
if (customers > 0)
{
cout << "customers accepted: " << customers << endl;
cout << " customers served: " << served << endl;
cout << " turnaways: " << turnaways << endl;
cout << "average queue size: ";
cout.precision(2);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << (double) sum_line / cyclelimit << endl;
cout << " average wait time: "
<< (double) line_wait / served << " minutes\n";
}
else
cout << "No customers!\n";
cout << "Done!\n"; return 0;
} // x = average time, in minutes, between customers
// return value is true if customer shows up this minute
bool newcustomer(double x)
{
return (std::rand() * x / RAND_MAX < 1);
}
第七题
#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm>
using namespace std;
vector<int> Lotto(int total,int choice); int main()
{
srand(time(0));
vector<int>winners;
winners=Lotto(51,6);
for(int i=0;i<6;i++)
{
cout<<winners[i]<<' ';;
}
cout<<endl;
return 0;
} vector<int> Lotto(int total,int choice)
{
vector<int> car(total);
for(int i=0;i<total;i++)
{
car[i]=i+1;
}
random_shuffle(car.begin(),car.end());
vector<int> cars(choice);
for(int i=0;i<choice;i++)
{
cars[i]=car[i];
}
sort(cars.begin(),cars.end());
return cars;
}
第八题
#include <iostream>
#include <vector>
#include <cstring>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std; set<string> Input_name(); int main()
{
set<string> M_f_n;
set<string> P_f_n;
set<string> U_f_n;
cout<<"开始输入Mat朋友的姓名\n";
M_f_n=Input_name();
copy(M_f_n.begin(), M_f_n.end(), ostream_iterator<string, char>(cout, " "));
cout<<endl;
cout<<"开始输入Pat朋友的姓名\n";
P_f_n=Input_name();
copy(P_f_n.begin(), P_f_n.end(), ostream_iterator<string, char>(cout, " "));
cout<<endl;
set_union(M_f_n.begin(),M_f_n.end(),P_f_n.begin(),P_f_n.end(),insert_iterator<set<string>>(U_f_n,U_f_n.begin()));
cout<<"开始输出并集\n"<<endl;
copy(U_f_n.begin(), U_f_n.end(), ostream_iterator<string, char>(cout, " "));
cout<<endl;
} set<string> Input_name()
{
set<string> name;
string name_f;
while(cin>>name_f)
{
name.insert(name_f);
if(cin.get()=='\n')
{
break;
}
}
return name;
}
第10题
#include <iostream>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <iterator>
#include <vector>
#include <fstream>
#include <ctime>
#include <list>
#include <queue>
#include <memory> using namespace std; struct Review
{
string title;
int rating;
double price;
};
bool FillRevice(Review & rr)
{
cout << "Enter book title (quit to quit): ";
getline(cin, rr.title);
if (rr.title == "quit")
{
return false;
}
cout << "Enter book rating: ";
cin >> rr.rating;
if (!cin)
{
return false;
} cout << "Enter book price: ";
cin >> rr.price;
if (!cin)
{
return false;
}
while (cin.get() != '\n')
continue; return true;
}
void ShowReview(const shared_ptr<Review> & rr)
{
cout << rr->rating << "\t" << rr->title << "\t" << rr->price << endl;
}
bool operator<(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
{
if (r1->title < r2->title)
{
return true;
}
else if (r1->title == r2->title && r1->rating < r2->rating)
return true;
else
return false;
}
bool worseThan(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
{
if (r1->rating < r2->rating)
return true;
else
return false;
}
bool betterThan(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
{
if (r1->rating > r2->rating)
return true;
else
return false;
}
bool expensiveThan(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
{
if (r1->price > r2->price)
return true;
else
return false;
}
bool cheaperThan(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
{
if (r1->price < r2->price)
return true;
else
return false;
}
void p16_10(void)
{
vector<shared_ptr<Review> > books;
Review temp;
while (FillRevice(temp))
{
shared_ptr<Review> in(new Review(temp));
books.push_back(in);
}
cout << "Enter your choice: " << endl;
cout << "0:按原始顺序显示, 1:按字母表顺序显示, 2:按评级升序显示" << endl;
cout << "3:按评级降序显示, 4:按价格升序显示 , 5:按价格降序显示" << endl;
cout << "6:退出" << endl;
int choice = 0;
vector<shared_ptr<Review> > original_books(books.begin(), books.end());
while (cin >> choice && choice != 6)
{
switch (choice)
{
case 0:
copy(original_books.begin(), original_books.end(), books.begin());
break; case 1:
sort(books.begin(), books.end());
break; case 2:
sort(books.begin(), books.end(), worseThan);
break; case 3:
sort(books.begin(), books.end(), betterThan);
break; case 4:
sort(books.begin(), books.end(), cheaperThan);
break; case 5:
sort(books.begin(), books.end(), expensiveThan);
break; default:
break;
} for_each(books.begin(), books.end(), ShowReview);
} return;
} int main(int argc, char ** argv)
{
p16_10(); while (cin.get()); return 0;
}
STL库相关练习代码的更多相关文章
- C/C++ 开源库及示例代码
C/C++ 开源库及示例代码 Table of Contents 说明 1 综合性的库 2 数据结构 & 算法 2.1 容器 2.1.1 标准容器 2.1.2 Lockfree 的容器 2.1 ...
- STL库list::sort()实现深度解析
原创,转载请注明出处:STL库list::sort()实现深度解析 list模板的定义以及一些基本成员函数的实现这里我就不赘述了,还不清楚的同学可以到网上查找相关资料或者直接查看侯捷翻译的<ST ...
- Xcode 生成静态库相关设置:
Xcode 生成静态库相关设置: #Build Setting1. Architectures ------- Architectures -----> $(ARCHS_STANDARD) -- ...
- [转] C++的STL库,vector sort排序时间复杂度 及常见容器比较
http://www.169it.com/article/3215620760.html http://www.cnblogs.com/sharpfeng/archive/2012/09/18/269 ...
- 使用STL库sort函数对vector进行排序
使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...
- STL的相关知识
STL简介: STL(Standard Template Library,标准模版库)以模板类和模版函数的形式为程序员提供了各种数据结构和算法的实现,程序员通过利用STL,可以在代码空间.执行时间和编 ...
- 模拟实现STL库
最近在复习STL,感觉再看的时候比刚开始学的时候通透很多.以前模拟实现了一个STL库,最近复习完又重构了一遍.代码放出来以供后面学习.如果有写的不好的地方欢迎大家批评指正. STL_List.h #p ...
- VC学习笔记----STL库
STL = Standard Template Library,标准模板库,惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Meng Lee和David R Muss ...
- 关于STL库中的max min swap
嗯... 不得不说c++中的STL库是一个神奇的东西 可以使你的代码显得更加简洁.... 今天就只讲STL中的三个鬼畜: max min swap 具体操作 ...
- MySQL数据库(二)--库相关操作、表相关操作(1)、存储引擎、数据类型
一.库相关操作 1.创建数据库 (1)语法 create database 数据库 charset utf8; (2)数据库命名规范 可以由字母.数字.下划线.@.#.$ 区分大小写 唯一性 不能使用 ...
随机推荐
- 排查前端接受后端的map产生的字段错误
报错内容 [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingExce ...
- jwt二次加密失败原因(Bad “options.expiresIn“ option the payload already has an “exp“ property.)
在写vue+nodejs项目的校验token时,出现了次错误 然后想了想问题所在: 第一次加密的时候使用jwt.sign(value,秘钥,{}),会返回一个字符串,然后当前端跳转别的发送请求时,会将 ...
- jdbc封装工具类(连接池)
c3p0配置文件: c3p0-config.xml <c3p0-config> <!-- 使用默认的配置读取连接池对象 --> <default-config> & ...
- 攻防世界-fileclude
攻防世界的一道文件包含题目 include("文件名"):会将文件中的内容视为代码块接入include所在代码中,输出的只是执行后的结果,文件中的注释.定义等无法查看. 本题中可以 ...
- centos7.2下配置DNS服务器
https://baijiahao.baidu.com/s?id=1748980460185046641&wfr=spider&for=pc 1.安装bind(服务器) yum -y ...
- 瞬间并发测试-jmeter
测试需求:秒杀场景,瞬间并发.通常来说,JMeter的线程数即为并发的压力数,实际上JMeter在运行时,每个线程是独立的,虽然有100个线程,但这些线程并不是同时向服务器发送请求,JMeter要模拟 ...
- select multiple 浏览器兼容
select multiple 时一般是设置 height <select multiple="multiple" style="height:87px;" ...
- ID生成器实现方式的优缺点比较以及最优的ID生成器原理剖析
引用:https://blog.csdn.net/luoyang_java/article/details/90679456 本文的重点主要是ID发号器相关的知识,介绍了雪花算法,以及他的基本原理和实 ...
- Java基于ssm师生实验课-实验室-实验设备预约系统源码
简介 java+ssm开发的实验课实验设备实验室预约系统,老师可预约实验设备和实验室,然后发布实验课和上传实验附件.学生可以报名实验课,也可以自己预约实验室(部分实验室对学生开放)做实验.学生做完实验 ...
- 305-基于XC7Z020的AI 人工智能 可编程相机
基于XC7Z020的AI 人工智能 可编程相机 一.产品概述 本产品为一款基于FPGA soc的支持二次开发的智能相机平台,基于大量已有的图形计算库和我们开发的支持库,用户可以使用python语言,轻 ...