VC10和C++ 0x (2) - auto

auto这个关键字来自C++ 98标准。在C++ 98中它没有什么作用,C++ 0x中“借用”它来作为自动类型推演(automatic type deduction)。当auto出现在声明中时,它表示“请用初始化我的表达式类型作为我的类型”。例如下面代码:

 

C:\Temp>type autocat.cpp


 1 #include <iostream>
 2 
 3 #include <map>
 4 
 5 #include <ostream>
 6 
 7 #include <regex>
 8 
 9 #include <string>
10 
11 using namespace std;
12 
13 using namespace std::tr1;
14 
15 int main() {
16 
17     map<stringstring> m;
18 
19 const regex r("(\\w+) (\\w+)");
20 
21     for (string s; getline(cin, s); ) {
22 
23         smatch results;
24 
25         if (regex_match(s, results, r)) {
26 
27             m[results[1]] = results[2];
28 
29         }
30 
31     }
32 
33     for (auto i = m.begin(); i != m.end(); ++i) {
34 
35         cout << i->second << " are " << i->first << endl;
36 
37     }
38 
39 }
40  

运行结果:

cute kittens

ugly puppies

evil goblins

^Z

kittens are cute

goblins are evil

puppies are ugly

 

上面例子中i的类型在编译时推演为 map<string, string>::iterator, 有了auto关键字你再也不用写又长又烦的代码了。

(注意 m.begin() 返回类型是 iterator, 而不是 const_iterator, 因为这里的m并不是const.  C++0x 中的cbegin() 能够解决这个问题,它返回non-const容器的const迭代器.)

 

lambda表达式和auto

前面的文章中提到了用tr1::functions来存储lambda表达式。 但是不建议那样做除非不得已,因为tr1::functions的开销问题。

 

如果你需要复用lambda表达式或者像给它命名,那么auto是更好的选择。

C:\Temp>type autolambdakitty.cpp

#include <algorithm>

#include <iostream>

#include <ostream>

#include <vector>

using namespace std;

template <typename T, typename Predicate> void keep_if(vector<T>& v, Predicate pred) {

    auto notpred = [&](const T& t) {

        return !pred(t);

    };

    v.erase(remove_if(v.begin(), v.end(), notpred), v.end());

}

template <typename Container> void print(const Container& c) {

    for_each(c.begin(), c.end(), [](const typename Container::value_type& e) { cout << e << " "; });

    cout << endl;

}

int main() {

    vector<int> a;

    for (int i = 0; i < 100; ++i) {

        a.push_back(i);

    }

    vector<int> b;

    for (int i = 100; i < 200; ++i) {

        b.push_back(i);

    }

    auto prime = [](const int n) -> bool {

        if (n < 2) {

            return false;

        }

        for (int i = 2; i <= n / i; ++i) {

            if (n % i == 0) {

                return false;

            }

        }

        return true;

    };

    keep_if(a, prime);

    keep_if(b, prime);

    print(a);

    print(b);

}

 

运行结果:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199

 

上面代码中notpred是一个lambda表达式的否定式。这个例子中我们不能够使用C++ 98的not1(),因为not1要求你的谓词是从unary_function派生的,但是lambda并不要求这点,所以很多情况下使用lambda更灵活。  

posted @ 2009-09-11 00:10  BruceJia  阅读(591)  评论(0编辑  收藏  举报