小编Quố*_*ờng的帖子

我对将值分配给std :: array的方式有一些疑问

我正在codedope.com上学习C ++,并且已经在另一个网站Learncpp.com上阅读了一份文档。但是这两个网站有不同的方法将值分配给数组。

//codesdope.com

std::array<int,5> n  {{1,2,3,4,5}};
Run Code Online (Sandbox Code Playgroud)
//learncpp.com

std::array<int,5> n = {1,2,3,4,5};
Run Code Online (Sandbox Code Playgroud)

哪种方法更准确?我应该选择哪种方式?它们之间有什么区别?

c++ c++11 c++14

6
推荐指数
1
解决办法
215
查看次数

我有一个关于将 std::array 传递到函数中的问题

std::array我正在学习 c ++ 并且将 a放入函数的语法让我感到困惑。

#include <iostream>
#include <array>

using namespace std;

void printArray(const std::array<int, 5> &n)
{
    std::cout << "length: " << n.size() << endl;

    for (int j = 0; j < n.size(); j++ )
    {
        cout << "n[" << j << "] = " << n[j] << endl;
    }

}

int main()
{

    array<int, 5> n = {1,2,3,4,5};

    printArray(n);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)
  1. 我想问一下const,它起什么作用,如果不使用它有什么作用?

  2. 当数组的名称已经是指针时,为什么我们必须使用 &n

c++

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

begin()和rend()有什么区别?

我正在学习“迭代器”,并且对begin()和rend()之间的区别有疑问。

#include <iostream>
#include <array>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> v1;
    v1 = {9,2,6,4,5};
    cout<<*v1.begin();
    cout<<*v1.rend();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

cout<<*v1.begin(); 返回9

cout<<*v1.rend(); 返回的数字不是9

为什么会有如此不同的结果?我需要一个答案,谢谢。

c++ iterator vector

1
推荐指数
3
解决办法
215
查看次数

标签 统计

c++ ×3

c++11 ×1

c++14 ×1

iterator ×1

vector ×1