vector非成员函数和成员函数的使用

vector非成员函数和成员函数的使用

非成员函数和成员函数的区别

class A{
public:
void f1(){}; // 这个就是成员函数。
void f2(); // 这个也是成员函数声明,其实现在类的外部。
};
void A::f2(){} // 这个是成员函数的实现。
void f3(){}; // 这个就是非成员函数,不属于任何一起其他的类。

vector中函数的使用

sort、for_each、random_shuffle

//===============================================================
//FileName:
//          vect3.cpp
//Date:
//          2019/11/26
//Author:
//          khoing(https://blog.csdn.net/qq_45391763)
//===============================================================
#include "iostream"
#include "string"
#include "vector"
#include <algorithm>

struct Review 
{
	std::string title;
	int rating;
};
//----------------------------------------------------------
bool FillReview(Review& rr);
void ShowReview(const Review& rr);
bool worseThan(const Review& r1, const Review& r2);
bool operator<(const Review& r1, const Review& r2);

int main() {
	using namespace std;

	vector<Review> books;
	Review temp;
//----------------------------------------------------------
	while (FillReview(temp))
		books.push_back(temp);
//----------------------------------------------------------
	if (books.size() > 0)
	{
		cout << "Thank you. You entered the following "
			<< books.size() << " ratings:\n"
			<< "Rating\tBook\n";
		//----------------------------------------------------------
		for_each(books.begin(), books.end(), ShowReview);//执行被指向的函数,
		//被指向的函数不能修改容器元素的值
		//----------------------------------------------------------
		//默认使用内置的升序排序
		sort(books.begin(), books.end());
		cout << "Sorted by title:\nRating\tBook\n";
		//----------------------------------------------------------
		for_each(books.begin(), books.end(), ShowReview);

		//也可以使用自定义的Operator<
		sort(books.begin(), books.end(), worseThan);//指向自定义的排序函数,
		cout << "Sorted by rating:\nRating\tBook\n";
		//----------------------------------------------------------
		for_each(books.begin(), books.end(), ShowReview);

		//----------------------------------------------------------
		random_shuffle(books.begin(), books.end());//随机排列
		cout << "After shuffling:\nRating\tBook\n";
		for_each(books.begin(), books.end(), ShowReview);
	}
	else
		cout << "No entries. ";
	cout << "Bye.\n";

}

bool FillReview(Review& rr)
{
	std::cout << "Enter book title (quit to quit): ";
//----------------------------------------------------------
	std::getline(std::cin, rr.title);
//----------------------------------------------------------
	if (rr.title == "quit") {

		return false;
	}

	std::cout << "Enter book rating: ";
	std::cin >> rr.rating;
//----------------------------------------------------------
	if (!std::cin)
		return false;
//----------------------------------------------------------
	// get rid of rest of input line
	while (std::cin.get() != '\n')//去掉行首的'\n'
		continue;
	return true;
	    
	
}

void ShowReview(const Review& rr)
{
	std::cout << rr.rating << "\t" << rr.title << std::endl;

}

bool worseThan(const Review& r1, const Review& r2)
{

	if (r1.rating < r2.rating)
		return true;
	else
		return false;
}

bool operator<(const Review& r1, const Review& r2)
{
	if (r1.title < r2.title)
		return true;
	else if (r1.title == r2.title && r1.rating < r2.rating)
		return true;
	else
		return false;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值