相关疑难解决方法(0)

如何使用std :: array模拟C数组初始化"int arr [] = {e1,e2,e3,...}"的行为?

(注意:这个问题是不必指定元素的数量,仍然允许直接初始化嵌套类型.)
这个问题讨论了C数组的用途int arr[20];.在他的回答中,@ James Kanze展示了C阵列的最后一个据点,它具有独特的初始化特性:

int arr[] = { 1, 3, 3, 7, 0, 4, 2, 0, 3, 1, 4, 1, 5, 9 };
Run Code Online (Sandbox Code Playgroud)

我们没有必要指定元素的数量,万岁!现在遍历它与C++ 11的功能std::beginstd::end<iterator>(或您自己的变体),你永远需要甚至认为它的大小.

现在,有没有(可能是TMP)方法实现同样的目标std::array?使用宏可以使它看起来更好.:)

??? std_array = { "here", "be", "elements" };
Run Code Online (Sandbox Code Playgroud)

编辑:中间版本,从各种答案编译,如下所示:

#include <array>
#include <utility>

template<class T, class... Tail, class Elem = typename std::decay<T>::type>
std::array<Elem,1+sizeof...(Tail)> make_array(T&& head, Tail&&... values)
{
  return { std::forward<T>(head), std::forward<Tail>(values)... };
}

// in code …
Run Code Online (Sandbox Code Playgroud)

c++ arrays templates initialization c++11

133
推荐指数
8
解决办法
1万
查看次数

基于范围的语句定义冗余

查看n3092,在§6.5.4中,我们找到了基于范围的for循环的等价性.然后它继续说什么__begin__end等于.它区分了数组和其他类型,我觉得这是多余的(也就是令人困惑).

它表示数组类型__begin__end你所期望的:指向第一个的指针和指向一个结尾的指针.那么对于其他类型的,__begin__end等于begin(__range)end(__range),与ADL.命名空间std是关联的,以便在第24.6.5节中找到std::beginstd::end定义<iterator>.

但是,如果我们看的定义std::beginstd::end,他们是阵列以及容器类型都定义.并且数组版本与上面完全相同:指向第一个的指针,指向一个结尾的指针.

为什么需要将数组与其他类型区分开来,当为其他类型提供的定义同样适用时,查找std::beginstd::end


为方便起见,有些删节报价:

§6.5.4基于范围的for陈述

- 如果_RangeT是一个数组类型,则begin-expr和end-expr分别是__range和__range + __bound,其中__bound是数组绑定的.如果_RangeT是未知大小的数组或不完整类型的数组,则程序格式错误.

- 否则,begin-expr和end-expr分别是begin(__ range)和end(__ range),其中begin和end通过参数依赖查找(3.4.2)查找.出于此名称查找的目的,名称空间std是关联的名称空间.

§24.6.5范围访问

template <class T, size_t N> T* begin(T (&array)[N]);
Run Code Online (Sandbox Code Playgroud)

返回:数组.

template <class T, size_t N> T* end(T (&array)[N]);
Run Code Online (Sandbox Code Playgroud)

返回:数组+ N.

c++ for-loop range argument-dependent-lookup c++11

19
推荐指数
1
解决办法
3459
查看次数

用旧方式迭代C++中的对象指针数组(没有基于范围的)?

我不是很精通C++,但是我得到了以下代码:

BaseManager* allManagers[] =
{
    mColorManager,
    mToolManager,
    mLayerManager,
    mPlaybackManager,
    mViewManager
};

for ( BaseManager* pManager : allManagers )
{
    pManager->setEditor( this );
    pManager->init();
}
Run Code Online (Sandbox Code Playgroud)

我使用较旧的g ++,所以我不能使用-std=c++11,我必须使用-std=c++0x.看看错误的"老派"等价物:在':'令牌之前的预期初始化程序,我希望以下内容可行:

for ( auto it = allManagers.begin(); it != allManagers.end(); ++it )
{
    BaseManager* pManager = *it;
    pManager->setEditor( this );
    pManager->init();
}
Run Code Online (Sandbox Code Playgroud)

...但它失败了:

error: request for member ‘begin’ in ‘allManagers’, which is of non-class type ‘BaseManager* [5]’
error: unable to deduce ‘auto’ from ‘<expression error>’ …
Run Code Online (Sandbox Code Playgroud)

c++ arrays

2
推荐指数
1
解决办法
5504
查看次数

简单的std :: sort无效

我有以下代码:

int main()
{
    int intArr[] = { 1,5,3 };
    //auto f = [](auto a, auto b) {return a < b;};
    //std::sort(intArr, intArr + 2, f);
    std::sort(intArr, intArr + 2);
    for (int& temp : intArr)
        cout << temp << endl;
}
Run Code Online (Sandbox Code Playgroud)

但是,输出未排序(例如输出1 5 3).std::sort与lambda一起使用时的结果相同.是什么导致了这种行为?

我正在使用Visual C++编译器(Visual Studio 2015).

c++ arrays sorting visual-c++ c++11

2
推荐指数
1
解决办法
249
查看次数