English 中文(简体)
C++11实验,为什么我能利用一些特点?
原标题:C++11 experiments, why can t I use some of the features?
  • 时间:2011-11-01 18:49:58
  •  标签:
  • c++
  • c++11

I m目前概述了C++11的新特征,目前原因不明,其中一些没有汇编。 根据海合会的联邦武装部队情况,我使用46.020100703(experimental)(GCC)版本,我所尝试的所有特征都是支持。 我试图用 st升到C++0x和 st=gnu++0x旗进行汇编。

Non member begin() & end()

例如,我wan不使用非成员,开始()和结束(),其成文法如下:

#include <iostream>
#include <map>
#include <utility>
#include <iterator>

using namespace std;
int main ( ) {
    map < string, string > alias;
    alias.insert ( pair < string, string > ( "ll", "ls -al" ) );
    // ... Other inserts

    auto it = begin(alias);
    while ( it != end(alias) ) {
        //...
    }

而且,我知道,

nonMemberBeginEnd//main.cc:15:24: error: ‘begin’ was not declared in this scope
nonMemberBeginEnd//main.cc:15:24: error: unable to deduce ‘auto’ from ‘<expression error>’ // Ok, this one is normal.
nonMemberBeginEnd//main.cc:16:26: error: ‘end’ was not declared in this scope

我是否需要包括特别负责人?

For range

My second (and last) question is weirder because It cannot depend on black magic hidden header that I might had not included.

The following Code :

for ( auto kv : alias )
    cout << kv.first << " ~ " << kv.second << endl;

下面的错误:

rangeFor/main.cc:15:17: error: expected initializer before ‘:’ token

我希望,我的问题不会成为你们的gu,也不要 too,你们会帮助我发现什么错误。

最佳回答

It works on gcc 4.6.1:

#include <iostream>
#include <map>
#include <string>

int main(int argc, char** argv) {
    std::map<std::string, std::string> alias = {{"key", "value"}};
    for (auto kv: alias)
        std::cout << kv.first << " ~ " << kv.second << std::endl;

    auto it = begin(alias);
    while (it != end(alias) ) {
        std::cout << (*it).first << " ~ " << (*it).second << std::endl;
        it++;
    }
    return EXIT_SUCCESS;
}

结果:

# /opt/gcc-4.6.1/bin/g++-4.6 --std=c++0x test.cc -o test && ./test
key ~ value
key ~ value
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签