小编Arm*_*gny的帖子

什么是尼布洛德?

使用 C++20,我们现在可以在 cppreference 中更频繁地阅读术语“niebloid”。

在 SO 上,我们今天可以找到 2020/07/16 2 篇提到它的文章:

谷歌也没有吐出那么多结果。最突出的也许是这里

有人可以更多地了解 niebloids 吗?

c++ c++20

75
推荐指数
1
解决办法
5914
查看次数

复制列表初始化?为什么要编译?

我使用的是 Microsoft Visual Studio Community 2019, V16.5.2。我想测试列表初始化

请参阅以下测试程序:

#include <string>

void foo(std::string str) {}

int main() {

    foo( {"str1", "str2"} );

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这编译没有错误和警告。为什么?

它给出了一个运行时错误: Expression: Transposed pointer range

有人可以解释一下这里发生了什么吗?


编辑。

我反汇编了代码并在调试器中运行它

    foo( {"str1", "str2"} );
00F739A8  sub         esp,1Ch  
00F739AB  mov         esi,esp  
00F739AD  mov         dword ptr [ebp-0C8h],esp  
00F739B3  lea         ecx,[ebp-0D1h]  
00F739B9  call        std::allocator<char>::allocator<char> (0F7136Bh)  
00F739BE  push        eax  
00F739BF  push        offset string "str2" (0F84DB8h)  
00F739C4  push        offset string "str1" (0F84E2Ch)  
00F739C9  mov         ecx,esi  
00F739CB  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> ><char const …
Run Code Online (Sandbox Code Playgroud)

c++

13
推荐指数
3
解决办法
280
查看次数

如何在编译时创建具有 string_views 序列的 constexpr 数组?

我想创建一个constexpr std::array<std::string_view, ConstexprNumber>. 例如,它应该包含constexpr std::strings_view's如下内容:

"text0", "text1", "text2", ..... "textn"

我想出了以下初始解决方案:

#include <iostream>
#include <array>
#include <utility>
#include <string>
#include <string_view>

// Number of strings that we want to generate
constexpr size_t NumberOfTextsToGenerate = 10u;

// constexpr function to build a string
constexpr std::string_view makeString(unsigned int i) {
    return std::string_view("text");
}

// Helper: constexpr function that will create an array of string_views and initialize it
template <unsigned int... ManyIntegers>
constexpr auto generateTextHelper(std::integer_sequence<unsigned int, ManyIntegers...>) {
    return …
Run Code Online (Sandbox Code Playgroud)

c++ arrays templates constexpr string-view

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

如何使用模板创建多维 std::array?

如何使用一些智能可变参数模板“MDA”创建具有(当然)已知初始尺寸的多维std::array类型。Typeconstexpr

维数应可变。

最后,我希望能够写:

MDA<int,3,4,5,6> a4d{};
Run Code Online (Sandbox Code Playgroud)

结果应该等于

std::array < std::array < std::array < std::array<int, 6> , 5 > , 4 > , 3 > a4d{};
Run Code Online (Sandbox Code Playgroud)

并节省大量(复杂的,甚至容易出错的)打字工作。. .


编辑:

我不确定这是否可能。但我正在寻找的是一些“打字保护程序”,可能与using声明结合使用。

c++ arrays templates variadic-templates

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

发送 EOF 后无法读取任何内容?

#include <stdio.h>
int main()
{
    char c = getchar(); //EOF (ctrl + d )
    while( ( c = getchar() ) != '?' )
    {
        printf( "%d\n", c == EOF );//infinite loop printing 1
    }
}
Run Code Online (Sandbox Code Playgroud)

这里会发生什么?

就好像 EOF 完全阻止读取其后的任何内容?

c infinite-loop eof getchar

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

C++ 将 CSV 文件解析为向量向量:丢失字符串第一个字符

我正在将 CSV 文件读入字符串向量向量中。我在下面写了代码。

#include<iostream>
#include<fstream>
#include<string>
#include <vector>
#include <fstream>
#include <cmath>
#include <sstream>

using namespace std;

int main()
{
    ifstream mesh;
    mesh.open("mesh_reference.csv");

    vector<vector<string> > point_coordinates;
    string line, word;

    while (getline(mesh,line))
    {
        stringstream ss(line);
        vector<string> row;
        while (getline(ss, word, ','))
        {
            row.push_back(word);
        }
        point_coordinates.push_back(row);
    }

    for(int i=0; i<point_coordinates.size(); i++) 
    {
        for(int j=0; j<3; j++)
            cout<<point_coordinates[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我打印出向量的向量时,我发现我在向量行的 0 位置丢失了 Element 的第一个字符。基本上,point_coordinates[0][0]显示的是 0.0001,而字符串应该是 -0.0001。我无法理解其原因。请帮忙。

典型的输出线是

 .0131 -0.019430324 0.051801
Run Code Online (Sandbox Code Playgroud)

而 CSV 数据是

0.0131,-0.019430324,0.051801
Run Code Online (Sandbox Code Playgroud)

文件中的 CSV …

c++ csv string file vector

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