`
javasalatu
  • 浏览: 718427 次
  • 性别: Icon_minigender_2
  • 来自: 北京
博客专栏
96df99eb-e89d-3228-9c8e-967fc745ec52
程序员的自我经营之道
浏览量:7665
文章分类
社区版块
存档分类
最新评论

remove_if函数的使用【转】

阅读更多

//////////////////////////////////////////////////////////////////////
//
// Compile options needed: -GX
//
// remove.cpp : This example shows how to use list::remove and
// list::remove_if. It also shows how to use
// list::remove_if with your own function.
//
// Functions:
//
// list::remove
// list::remove_if
//
// Written by Andrew Bradnan
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////


#pragma warning(disable:4786) // disable spurious C4786 warnings

#include <list>
#include <string>
#include <iostream>
using namespace std;

#if _MSC_VER > 1020 // if later than revision 4.2
using namespace std; // std c++ libs are implemented in std
#endif

typedef list<string, allocator<string> > LISTSTR;

// Used to customize list::remove_if()
class is_four_chars
: public not_equal_to<string>
{
bool operator()(const string& rhs, const string&) const
{ return rhs.size() == 4; }
};

void main()
{
LISTSTR test;
LISTSTR::iterator i;

test.push_back("good");
test.push_back("bad");
test.push_back("ugly");

// good bad ugly
for (i = test.begin(); i != test.end(); ++i)
cout << *i << " ";
cout << endl;

test.remove("bad");

// good ugly
for (i = test.begin(); i != test.end(); ++i)
cout << *i << " ";
cout << endl;

// remove any not equal to "good"
test.remove_if(binder2nd<not_equal_to<string> >
(not_equal_to<string>(), "good"));

// good
for (i = test.begin(); i != test.end(); ++i)
cout << *i << " ";
cout << endl;

// Remove any strings that are four characters long
test.remove_if(binder2nd<not_equal_to<string> >
(is_four_chars(), "useless parameter"));

if (test.empty())
cout << "Empty list\n";

}

http://support.microsoft.com/kb/168047/zh-tw

分享到:
评论

相关推荐

    《深入学习c++string》2.1版

    1.5.1 string与remove 13 1.5.2 string与unique、sort 13 1.5.3 string与search 13 1.5.4 string和find、find_if 14 1.5.5 string与copy、copy_if 14 1.5.6 string与count、count_if 15 1.6 string与wstring 15 ...

    -C++参考大全(第四版) (2010 年度畅销榜

    34.37 remove,remove_if,remove copy和remove_copy_if 34.38 replace,replace_copy,replace_if和replace_copy_if 34.39 reverse和reverse_copy 34.40 rotate和rotate_copy 34.41 search 34.42 search_n 34.43 ...

    stl详解 包括各种实例代码

    5. remove / remove_if 30 6. unique 31 7. rotate 32 8. random_shuffle 32 9. partition / stable_partition 33 10. sort / stable_sort 33 11. partial_sort 34 12. nth_element 34 13. lower_bound / upper_...

    将wkai的最短路径由经典改为A星算法

    除去表中的重复项,本例程未用到此函数 (defun remove:same (lst / re) (foreach n lst (if (member n re) () (setq re (append re (list re))) ) ) re ) ;;获得实体的长度 (defun get:len (e) (vlax-curve-...

    C++ STL 开发技术导引(第6章)

    22.18 条件移除remove_if 321 22.19 不连续重复元素复制unique_copy 322 22.20 剔除连续重复元素unique 324 22.21 元素反向reverse 325 22.22 反向复制reverse_copy 326 22.23 旋转rotate 327 22.24 ...

    C++ STL开发技术导引(第5章)

    22.18 条件移除remove_if 321 22.19 不连续重复元素复制unique_copy 322 22.20 剔除连续重复元素unique 324 22.21 元素反向reverse 325 22.22 反向复制reverse_copy 326 22.23 旋转rotate 327 22.24 ...

    C++ STL开发技术导引(第3章)

    22.18 条件移除remove_if 321 22.19 不连续重复元素复制unique_copy 322 22.20 剔除连续重复元素unique 324 22.21 元素反向reverse 325 22.22 反向复制reverse_copy 326 22.23 旋转rotate 327 22.24 ...

    effective stl 中文 pdf

    条款36: 用not1和remove_copy_if来表现copy_if 条款37: 用accumulate或for_each来统计序列 仿函数,仿函数类,函数等等 条款38: 把仿函数类设计成值传递的 条款39: 用纯函数做predicate 条款40: 增强仿函数类的...

    Vector用法介绍

    这篇文章的目的是为了介绍...本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用。通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了。

    unix分析关于UNIX的一些浅析

    /*将device_driver中的probe,remove,shutdown函数指针指向platform_driver中的函数,后面进行驱动和设备绑定后会调用probe函数 */ if (drv-&gt;probe) drv-&gt;driver.probe = platform_drv_probe; if (drv-&gt;...

    Effictive STL CHM中文版

    条款36: 用not1和remove_copy_if来表现copy_if 条款37: 用accumulate或for_each来统计序列 仿函数,仿函数类,函数等等 条款38: 把仿函数类设计成值传递的 条款39: 用纯函数做predicate 条款40: 增强仿函数类的...

    C++大学教程,一本适合初学者的入门教材(part2)

    20.5.3 remove、remove_if、 remove_copy和remove_copy_if 20.5.4 replace、replace_if、replace_copy和replace_copy_if 20.5.5 数学算法 20.5.6 基本查找与排序算法 20.5.7 swap、iter_swap和swap_ranges...

    C++大学教程,一本适合初学者的入门教材(part1)

    20.5.3 remove、remove_if、 remove_copy和remove_copy_if 20.5.4 replace、replace_if、replace_copy和replace_copy_if 20.5.5 数学算法 20.5.6 基本查找与排序算法 20.5.7 swap、iter_swap和swap_ranges...

    STL源码剖析.pdg

    remove_if 357 remove_copy_if 358 replace 359 replace_copy 359 replace_if 359 replace_copy_if 360 reverse 360 reverse_copy 361 rotate 361 rotate_copy 365 search 365 search_n 366 swap_ranges...

    php 删除数组元素

    删除数组中为空值的元素就可以使用这个函数。 复制代码 代码如下://删除数组中的一个元素 function array_remove_value(&$arr, $var){ foreach ($arr as $key =&gt; $value) { if (is_array($value)) { array_remove_...

    STL 源码剖析(侯捷先生译著)

    remove_if 357 remove_copy_if 358 replace 359 replace_copy 359 replace_if 359 replace_copy_if 360 reverse 360 reverse_copy 361 rotate 361 rotate_copy 365 search 365 search_n 366 swap_ranges...

    Python之高级函数-高级程序员与普通程序员的区别

    目录 python的高阶函数 1、假如你想从一个考试分数的列表中删除所有的0分,怎么办? 普通程序员: 高级程序员: 2、filter函数 3、给定一个数列[1,3,5,6,7,9,10,435],求数列中每一项相乘的结果 ... if numbe

    深入浅析STL vector用法

    本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用。通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了。 Vector总览 vector是...

    《C++String深入详解2.0版》PDF

    1.5.1 string与remove 12 1.5.2 string与unique、sort 12 1.5.3 string与search 12 1.5.4 string和find、find_if 13 1.5.5 string与copy、copy_if 13 1.5.6 string与count、count_if 14 1.6 string与wstring 14 ...

    Python如何在循环内使用list.remove()

    if item == '0': dat.remove(item) print(dat) #按要求是把'0'都删掉的,输出结果是['1', '2', '3', '0'] ?? 首先,remove(x) 移除的是序列首次碰到的元素x 理解: 遍历列表,item每一次都会变化,可以想象有一...

Global site tag (gtag.js) - Google Analytics