还是我们公司给新人培训C++的问题, 有一个作业是这样的:
从控制台读入一些字符串,然后放到链表里(这个自己实现), 然后排序输出.
培训负责人的目的是联系一下指针, 链表, 还有看一下编码规范, 还是释放内存等细节.
当然, 如若撇去这些目的, 这样一个功能, 我可能写出这样的代码:
#include <string.h>
#include <list>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main() {
list<string> strs;
//input
cout<<"please input ten string, and press CTRL+Z to press."<<endl;
istream_iterator<string> input(cin);
istream_iterator<string> end_of_stream;
copy(input,end_of_stream,inserter(strs,strs.begin()));
//sort
strs.sort();
//output
cout << "the sorted result is below." << endl;
copy(strs.begin(), strs.end(), ostream_iterator<string > (cout, "\n"));
return 0;
}
用gcc 3.2.3一编译, 没问题, 跑得很不错.
但在vc2008里面一跑, 傻了, 说cin 没有>>string这个方法.
查了一下, 原来cin>> string确实不是标准的做法.
看来gcc也..... 呵呵, 虽然我很喜欢他 :)
记录一下:
standards在线查看地址:
http://www.csci.csusb.edu/dick/c++std/#Drafts
http://www.csci.csusb.edu/dick/c++std/cd2/index.html
当然还可以google到pdf版本的,下面是一个可下载链接.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
记得C同学告诉过我, 这个标准的打印版要19$,
所以很显然, 这篇blog里面最有价值的东西也就是这个几个链接了. 其余的都是废话. :)