[阅读: 209] 2009-02-13 02:25:20
The auto keyword from C++98, where it did absolutely nothing, has been repurposed in C++0x for automatic type deduction. When used in a declaration, it says "make the type of this thing the same as the type of whatever initializes it". Behold:
C:\Temp>type autocat.cpp
#include <iostream>
#include <map>
#include <ostream>
#include <regex>
#include <string>
using namespace std;
using namespace std::tr1;
int main() {
map<string, string> m;
const regex r("(\\w+) (\\w+)");
for (string s; getline(cin, s); ) {
smatch results;
if (regex_match(s, results, r)) {
m[results[1]] = results[2];
}
}
for (auto i = m.begin(); i != m.end(); ++i) {
cout << i->second << " are " << i->first << endl;
}
}
C:\Temp>cl /EHsc /nologo /W4 autocat.cpp > NUL && autocat
cute kittens
ugly puppies
evil goblins
^Z
kittens are cute
goblins are evil
puppies are ugly
map<string, string>::iterator, your decade-long reign of terror has come to an end!
(Note that m.begin() returns iterator, not const_iterator, because the map is not const. C++0x cbegin() allows you to request a const_iterator from a non-const container.)

机器人,
这首歌学会了没有?

我们的目标是->没有蛀牙!
