C++ Primer 学习笔记_41_STL实践与分析(15)--初窥算法(二)

2014-11-24 12:04:08 · 作者: · 浏览: 1

【注意:】

words的大小并没有改变,依然保存着10个元素;只是这些元素的顺序改变了。unique实际上并没有删除任何元素,而是将无重复的元素复制到序列的前端,从而覆盖相邻的重复元素

3)、使用容器操作删除元素

调用erase实现删除重复的项。这个函数调用从end_unique指向的元素开始删除,直到words的最后一个元素也删除掉为止。调用之后,words存储输入的 8个不相同的元素。

算法不直接修改容器的大小。如果需要添加或删除元素,则必须使用容器操作。【好像有点不负责任的意思O(∩_∩)O~】

4)、定义需要的实用函数

两个接下来要使用的函数stable_sort和count_if。这两个是使用stable_sort和count_if算法的配套函数,称为谓词。谓词是做某些检测的函数,返回用于条件判断的类型,指出条件是否成立。

bool isShorter(const string &s1,const string &s2)
{
    return s1.size() < s2.size();
}
bool GT6(const string &s)
{
    return s.size() >= 6;
}

5)、排序算法

除了sort之外,标准库还定义了stable_sort算法,stable_sort保留相等元素的原始相对位置。

sort和 stable_sort都是重载函数。其中一个版本使用元素类型提供的小于(<)操作符实现比较。第二个重载版本带有第三个形参:比较元素所使用的谓词函数的名字。这个谓词函数必须接受两个实参,实参的类型必须与元素类型相同,并返回一个可用作条件检测的值。如:

stable_sort(words.begin(),words.end(),isShorter);

调用后:

fox

red

the

over

slow

jumps

quick

turtle

6、统计长度不小于6的单词数

使用count_if算法实现:

    vector
  
   ::size_type cnt = count_if(words.begin(),words.end(),GT6);

  

执行count_if函数时,首先读取他的头两个参数所标记的范围内的元素,每读取一个元素,则将其传递给第三个实参表示的谓词函数。

7、将全部程序放在一起

bool isShorter(const string &s1,const string &s2)
{
    return s1.size() < s2.size();
}
bool GT6(const string &s)
{
    return s.size() >= 6;
}

int main()
{
    vector
  
    words;
    ifstream inFile("input");

    string word;
    while (inFile >> word)
    {
        words.push_back(word);
    }

    sort(words.begin(),words.end());
    vector
   
    ::iterator end_unique = unique(words.begin(),words.end()); words.erase(end_unique,words.end()); stable_sort(words.begin(),words.end(),isShorter); vector
    
     ::size_type wc = count_if(words.begin(),words.end(),GT6); cout << wc << " words 6 characters or longer." << endl; cout << "Words:" << endl; for (vector
     
      ::iterator it = words.begin(); it != words.end(); ++it) { cout << *it << endl; } } 
     
    
   
  

//P347 习题11.9
bool GT4(const string &str)
{
    return str.size() >= 4;
}

int main()
{
    ifstream inFile("input");
    vector
  
    strVec;
    string str;

    while (inFile >> str)
    {
        strVec.push_back(str);
    }

    sort(strVec.begin(),strVec.end());
    vector
   
    ::iterator end_unique = unique(strVec.begin(),strVec.end()); vector
    
     ::size_type word_cnt = count_if(strVec.begin(),end_unique,GT4); cout << "Now! Have " << word_cnt << " words made of 4 characters or longer." << endl; for (vector
     
      ::iterator iter = strVec.begin(); iter != end_unique; ++iter) { cout << *iter << endl; } } 
     
    
   
  

//习题11.10
bool GT6(const string &str)
{
    return str.size() >= 6;
}

int main()
{
    ifstream inFile("input");
    vector
  
    strVec;
    string str;

    while (inFile >> str)
    {
        strVec.push_back(str);
    }

    vector
   
    ::size_type word_cnt = 0; vector
    
     ::iterator first = strVec.begin(); while ((first = find_if(first,strVec.end(),GT6)) != strVec.end()) { ++ word_cnt; ++ first; } cout << word_cnt << endl; cout << "WORD:" << endl; for (vector
     
      ::iterator iter = strVec.begin(); iter != strVec.end(); ++iter) { cout << *iter << endl; } }