STL 对矢量的其他操作

for_each()
可以替代要显示用iterator的循环

ep:
vector<Review>::iterator pr;
for(pr=books.bgein();pr!=books.end();pr++)  
   ShowReview(*pr);
************************************************************
for_each(books.begein(),books.end(),ShowReview);
      

////////////////////////////////////////////////////////////////////

Random_shuffle()
随机排列容器中的元素  //要求容器支持随机访问

ep:
Random_shuffle(books.begein(),books.end());

////////////////////////////////////////////////////////////////////

sort() 
按<排序,即按升序 //要求容器支持随机访问 并 有2个版本

ep1:
vector<int> coolstuff;

sort(coolstuff.begein(),coolstuff.end());

如容器元素是自定义的 则需要对其opertor<()重载

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;
}
**************************************************************

ep2:
想按自己的要求排序 则需要用3个参数的

按降序

先自定义一个函数
bool WorseThan (const Review & r1,const Review & r2)
{
   if (r1.rating<r2.rating)
      return false;
   else
      return true;
}
然后调用
sort(books.begein(),books.end(),WorseThan);

 

 

/////////////////////////////////////////////////////////////
//
//vector3.cpp
//
////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct Review {
    std::string title;
    int rating;
};

bool operator<(const Review & r1, const Review & r2);
bool worseThan(const Review & r1, const Review & r2);
bool FillReview(Review & rr);
void ShowReview(const Review & rr);
int main()
{
    using namespace std;

 vector<Review> books;
    Review temp;
    while (FillReview(temp))
        books.push_back(temp);
    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());           //sort(a,a)
    cout << "Sorted by title:\nRating\tBook\n";
    for_each(books.begin(), books.end(), ShowReview);

    sort(books.begin(), books.end(), worseThan); //sort(a,a,a)
    cout << "Sorted by rating:\nRating\tBook\n";
    for_each(books.begin(), books.end(), ShowReview);

    random_shuffle(books.begin(), books.end());//random_shuffle()
    cout << "After shuffling:\nRating\tBook\n";
    for_each(books.begin(), books.end(), ShowReview);
    cout << "Bye.\n";
    cin.get();
    return 0;
}

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;
}

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

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;
    std::cin.get();
    return true;
}

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

posted @ 2007-03-17 20:04  Edward Xie  阅读(125)  评论(0编辑  收藏  举报