c++11 体验

来源:互联网 发布:99热网址最新获取域名 编辑:程序博客网 时间:2024/06/14 00:45
#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

int main()
{
    int arry[] = {1,2,3,4,5,6,7,8,9,0,11,12};
    std::set<int> one(arry, arry+7);
    std::set<int> two(arry + 4, arry+12);
    std::set<int> diff;

    std::cout << "one array values: " <<endl;
    for_each(one.begin(), one.end(), [](int value) {
            std::cout << value << ", ";});
    std::cout <<std::endl;

    std::cout << "two array values: " <<endl;
    for_each(two.begin(), two.end(), [](int value) {
            std::cout << value << ", ";});
    std::cout <<std::endl;

    std::set_difference(one.begin(), one.end(), two.begin(), two.end(), std::inserter(diff,diff.begin()));

    std::cout << "diff array values: " <<endl;
    for_each(diff.begin(), diff.end(), [](int value) {
            std::cout << value << ", ";});
    std::cout <<std::endl;

    diff.clear();

    std::set_difference(two.begin(), two.end(), one.begin(), one.end(), std::inserter(diff,diff.begin()));

    std::cout << "diff array values: " <<endl;
    for_each(diff.begin(), diff.end(), [](int value) {
            std::cout << value << ", ";});
    std::cout <<std::endl;

    return 0;

}

编译:

g++ main.cpp -std=c++0x

系统环境:

os:Ubuntu 12.04.2 LTS

g++: gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 

开始使用g++ main.cpp直接编译出错,提示不支持c++11特性, 编译时加上-std=c++0x


输出:

one array values:
1, 2, 3, 4, 5, 6, 7,
two array values:
0, 5, 6, 7, 8, 9, 11, 12,
diff array values:
1, 2, 3, 4,
diff array values:
0, 8, 9, 11, 12,