Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用不同的值填充数组/向量的边和中心_C++_C++17 - Fatal编程技术网

C++ 用不同的值填充数组/向量的边和中心

C++ 用不同的值填充数组/向量的边和中心,c++,c++17,C++,C++17,我想创建一个函数,它初始化一个大小为width*height的向量或数组,但也在这些值周围创建一个边框 外部周围的值也需要初始化为与中心不同的值 我存储的对象没有默认构造函数,因此我无法依赖它进行初始化 这是我到目前为止的代码,但感觉应该有一种更简单或更惯用的方法来实现这一点 我可以使用任何功能,包括C++1z #include <iostream> #include <vector> void fill_values(const unsigned width, con

我想创建一个函数,它初始化一个大小为
width*height
的向量或数组,但也在这些值周围创建一个边框

外部周围的值也需要初始化为与中心不同的值

我存储的对象没有默认构造函数,因此我无法依赖它进行初始化

这是我到目前为止的代码,但感觉应该有一种更简单或更惯用的方法来实现这一点

我可以使用任何功能,包括C++1z

#include <iostream>
#include <vector>

void fill_values(const unsigned width, const unsigned height, std::vector<int> &values) {
    for(unsigned y=0; y<height+2; ++y) {
        for(unsigned x=0; x<width+2; ++x) {
            if(x==0 || x==width+1 || y==0 || y==height+1) {
                values.push_back(1);
            } else {
                values.push_back(0);
            }
        }
    }
}

int main(int argc, char *argv[]) {
    const unsigned width = 4;
    const unsigned height = 3;
    std::vector<int> values;

    fill_values(width, height, values);

    for(unsigned y=0; y<height+2; ++y) {
        for(unsigned x=0; x<width+2; ++x) {
            std::cout << values[y * (width+2) + x];
        }
        std::cout << '\n';
    }

    return 0;
}

正如@Fedorico在评论中所说,使用向量向量是更好的表示变量值的方法。与其通过引用将值作为参数传递,不如依赖于返回值。我还发现使用设置的高度和宽度作为数据中的行和列的总数更容易,因此不需要添加两个

以下代码取决于c++11或更新版本:

#include <iostream>
#include <vector>

using namespace std;

// Fills the 2D matrix with 1s on the border and 0s in the middle.
vector<vector<int>> generate_matrix(int rows, int cols);
void print_matrix(const vector<vector<int>>& matrix);

int main()
{
    // Don't sync I/O with C stdio.
    ios_base::sync_with_stdio(false);

    // Height and Width of the entire 2D matrix.
    const int rows = 6;
    const int cols = 5;

    vector<vector<int>> matrix = generate_matrix(rows, cols);
    print_matrix(matrix);

    return 0;
}

vector<vector<int>> generate_matrix(int rows, int cols)
{
    // fill a rows x cols 2D vector with 0s.
    vector<vector<int>> matrix(rows, vector<int>(cols, 0));

    // fill in 1s on top and bottom rows.
    if (rows > 0)
    {
        for (int i = 0; i < cols; ++i)
        {
            matrix[0][i] = 1;
            matrix[rows-1][i] = 1;
        }
    }

    // fill in 1s on the left and right columns.
    if (cols > 0)
    {
        for (int i = 0; i < rows; ++i)
        {
            matrix[i][0] = 1;
            matrix[i][cols-1] = 1;
        }
    }

    return matrix;
}

void print_matrix(const vector<vector<int>>& matrix)
{
    // Use a reference for the row iterator to prevent a vector copy.
    for (auto& row : matrix)
    {
        for (auto element : row)
        {
            cout << element;
        }
        cout << '\n';
    }
}
#包括
#包括
使用名称空间std;
//填充边界上的1s和中间的0s的2D矩阵。
向量生成_矩阵(int行,int列);
无效打印矩阵(常量向量和矩阵);
int main()
{
//不与C stdio同步I/O。
ios_base::与_stdio同步(false);
//整个二维矩阵的高度和宽度。
const int rows=6;
常数int cols=5;
向量矩阵=生成矩阵(行、列);
打印矩阵(矩阵);
返回0;
}
向量生成_矩阵(int行,int列)
{
//用0填充行x列2D向量。
向量矩阵(行,向量(cols,0));
//在顶行和底行填写1。
如果(行数>0)
{
对于(int i=0;i0)
{
对于(int i=0;icout正如@Fedorico在评论中所说的,使用向量向量更能表示您的值变量。与其通过引用传递值作为参数,不如依赖于返回值。我还发现,使用设置的高度和宽度作为数据变量中的行和列的总数更容易o不需要添加两个

以下代码取决于c++11或更新版本:

#include <iostream>
#include <vector>

using namespace std;

// Fills the 2D matrix with 1s on the border and 0s in the middle.
vector<vector<int>> generate_matrix(int rows, int cols);
void print_matrix(const vector<vector<int>>& matrix);

int main()
{
    // Don't sync I/O with C stdio.
    ios_base::sync_with_stdio(false);

    // Height and Width of the entire 2D matrix.
    const int rows = 6;
    const int cols = 5;

    vector<vector<int>> matrix = generate_matrix(rows, cols);
    print_matrix(matrix);

    return 0;
}

vector<vector<int>> generate_matrix(int rows, int cols)
{
    // fill a rows x cols 2D vector with 0s.
    vector<vector<int>> matrix(rows, vector<int>(cols, 0));

    // fill in 1s on top and bottom rows.
    if (rows > 0)
    {
        for (int i = 0; i < cols; ++i)
        {
            matrix[0][i] = 1;
            matrix[rows-1][i] = 1;
        }
    }

    // fill in 1s on the left and right columns.
    if (cols > 0)
    {
        for (int i = 0; i < rows; ++i)
        {
            matrix[i][0] = 1;
            matrix[i][cols-1] = 1;
        }
    }

    return matrix;
}

void print_matrix(const vector<vector<int>>& matrix)
{
    // Use a reference for the row iterator to prevent a vector copy.
    for (auto& row : matrix)
    {
        for (auto element : row)
        {
            cout << element;
        }
        cout << '\n';
    }
}
#包括
#包括
使用名称空间std;
//填充边界上的1s和中间的0s的2D矩阵。
向量生成_矩阵(int行,int列);
无效打印矩阵(常量向量和矩阵);
int main()
{
//不与C stdio同步I/O。
ios_base::与_stdio同步(false);
//整个二维矩阵的高度和宽度。
const int rows=6;
常数int cols=5;
向量矩阵=生成矩阵(行、列);
打印矩阵(矩阵);
返回0;
}
向量生成_矩阵(int行,int列)
{
//用0填充行x列2D向量。
向量矩阵(行,向量(cols,0));
//在顶行和底行填写1。
如果(行数>0)
{
对于(int i=0;i0)
{
对于(int i=0;i虽然差别不大,但您可以将
std::generate_n()
(从c++11开始)与lambda函数一起使用

下面是一个完整的工作示例

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

int main ()
 {
    constexpr std::size_t  width  { 4U };
    constexpr std::size_t  height { 3U };
    constexpr std::size_t  w2     { width + 2U };
    constexpr std::size_t  h2     { height + 2U };

    std::vector<int> values;

    values.resize ( w2 * h2 );

    std::generate_n(values.begin(), w2 * h2, [=]() -> int
     { 
       static std::size_t i = -1;

       ++i;

       return     ( 0U == i / w2 ) || ( h2 - 1U == i / w2 ) 
               || ( 0U == i % w2 ) || ( w2 - 1U == i % w2 );
     });

    for(unsigned y=0; y<height+2; ++y) {
        for(unsigned x=0; x<width+2; ++x) {
            std::cout << values[y * (width+2) + x] << ' ';
        }
        std::cout << '\n';
    }

    return 0;
}
#包括
#包括
#包括
int main()
{
constexpr std::size_t width{4U};
constexpr std::大小和高度{3U};
constexpr std::size_t w2{width+2U};
constexpr std::size_t h2{height+2U};
std::向量值;
value.resize(w2*h2);
std::generate_n(values.begin(),w2*h2,[=]()->int
{ 
静态标准::大小i=-1;
++一,;
返回(0U==i/w2)| |(h2-1U==i/w2)
||(0U==i%w2)| |(w2-1U==i%w2);
});

for(unsigned y=0;y差别不大,但可以将
std::generate_n()
(从c++11开始)与lambda函数一起使用

下面是一个完整的工作示例

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

int main ()
 {
    constexpr std::size_t  width  { 4U };
    constexpr std::size_t  height { 3U };
    constexpr std::size_t  w2     { width + 2U };
    constexpr std::size_t  h2     { height + 2U };

    std::vector<int> values;

    values.resize ( w2 * h2 );

    std::generate_n(values.begin(), w2 * h2, [=]() -> int
     { 
       static std::size_t i = -1;

       ++i;

       return     ( 0U == i / w2 ) || ( h2 - 1U == i / w2 ) 
               || ( 0U == i % w2 ) || ( w2 - 1U == i % w2 );
     });

    for(unsigned y=0; y<height+2; ++y) {
        for(unsigned x=0; x<width+2; ++x) {
            std::cout << values[y * (width+2) + x] << ' ';
        }
        std::cout << '\n';
    }

    return 0;
}
#包括
#包括
#包括
int main()
{
constexpr std::size_t width{4U};
constexpr std::大小和高度{3U};
constexpr std::size_t w2{width+2U};
constexpr std::size_t h2{height+2U};
std::向量值;
value.resize(w2*h2);
std::generate_n(values.begin(),w2*h2,[=]()->int
{ 
静态标准::大小i=-1;
++一,;
返回(0U==i/w2)| |(h2-1U==i/w2)
||(0U==i%w2)| |(w2-1U==i%w2);
});

对于(unsigned y=0;y老实说,您的代码很好。我很容易理解它的功能

但本着提出其他复杂实现的精神,我建议如下。填充矩阵的另一种方法是添加一整行1,然后添加
height
1000…001
,然后再添加一整行1。我们可以使其更加明确。此外,建议返回一个
向量
,而不是fi它:

std::vector<int> fill_values(const unsigned width, const unsigned height) {
    std::vector<int> m;
    m.reserve((width + 2) * (height + 2));

    // add row of 1s
    m.insert(m.end(), width + 2, 1);

    // add height middle rows
    for (int i = 0; i < height; ++i) {
        m.push_back(1);
        m.insert(m.end(), width, 0);
        m.push_back(1);
    }

    // and a final row of 1s
    m.insert(m.end(), width + 2, 1);

    return m;
}
std::向量填充值(常量无符号宽度、常量无符号高度){
std::向量m;
m、 预留空间((宽度+2)*(高度)+