小编Nut*_*ker的帖子

如何在 C++ 中获取(无序)集合差异或对称差异?

我无法推断我可以使用文档中的std::set_difference,因为它说集合应该排序,这意味着它们不是集合,而是列表。此外,所有示例都是关于有序列表,而不是集合。

如何知道真相?

c++ set set-difference

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

如何在编译时创建具有 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
查看次数

打印到PDF时以字符串格式退格

我正在尝试在PDF中打印两列信息,其中包含用户输入的一些字符串。到目前为止,这是我的代码:

string s = "";
int width = 60 - name.Count(Char.IsWhiteSpace);                                                                  
s = s + string.Format("{0,-" + width +"}", "Name: " + name);
s = s + string.Format("{0,15}","AAA: ");
p1.Add(s);
document.Add(p1);

string p = "";
int width = 60 - surname.Count(Char.IsWhiteSpace);                                                                 
p = p + string.Format("{0,-"+ width +"}", "Surname: " + surname);
p = p + string.Format("{0,15}","BBB: ");
p2.Add(p);
document.Add(p2);

string r = ""; 
int width = 60 - school.Count(Char.IsWhiteSpace);                                                       
r = r + string.Format("{0,-"+ width +"}", "School: " + …
Run Code Online (Sandbox Code Playgroud)

c# pdf string formatting itextsharp

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

有没有可能 make_shared 没有任何异常但返回一个 nullptr?

我最近正在处理 shared_ptr 的问题。我很好奇如果 make_shared 失败,它会引发异常吗?是否有任何一种情况 make_shared 返回 nullptr 但没有任何例外?

c++ shared-ptr make-shared c++11

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

为什么表的第二行不会被写入?

我正在尝试在 PDF 文档顶部创建 4 列 2 行的无边框表格。问题是第二行不会被写入。这是我的代码:

float[] columnWidths = { 2, 1, 1, 1};
PdfPTable table = new PdfPTable(columnWidths);
table.WidthPercentage = 100;
if (...) //true
{
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("AAA:_______________",infoFont));    
                p.BorderWidth = 0;  
                table.AddCell(p);  // fixed pos. 1st col,1st row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("BBB:_____", infoFont));   
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 2nd col,1st row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("CCC:_____", …
Run Code Online (Sandbox Code Playgroud)

c# pdf itext

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

在 C++ 模板参数中不使用 constexpr

我正在使用类型为 的变量itk::Image<OutputPixelType, Dimension>,其中“itk”来自图像处理库 ITK。

以下代码编译:

constexpr unsigned int Dimension = 3;
using PixelType = float; 
using MyImageType = itk::Image<PixelType, Dimension>;
Run Code Online (Sandbox Code Playgroud)

但现在我需要将“维度”定义为从函数计算出来的东西。

unsigned int Dimension = get_dimension(...);
Run Code Online (Sandbox Code Playgroud)

我的编译器报错:

error: non-type template argument is not a constant expression
  using MyImageType = itk::Image<PixelType, Dimension>;
                                            ^~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我希望使用“维度”作为从函数计算出来的东西。

c++ templates constexpr itk c++11

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

在模板函数中,如果输入的类型是枚举类,我该如何使用 std::underlying_type?

我有一段代码返回给定数字的某些位的值(我也使用 static_cast 将枚举类计算为一个数字)。

template<typename Type>
bool get_bits(Type input, uint8_t offset, uint8_t n, Type* destination)
{
    if (offset + n> sizeof(Type) * 8)
        return false;

    Type bitmask = 0;

    for (int i = 0; i < n; ++i)
        bitmask |= (1 << i);

    *destination = static_cast<Type>(input >> offset & bitmask);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

该函数试图返回的值n的位input由开始offset。它适用于整数类型,但是当我尝试将它与枚举类一起使用时,编译失败。我尝试使用std::underlying_type然后它适用于枚举类但不适用于整数类型:|。我怎样才能将它用于他们两个?我的意思是,如果类型是枚举类,我想将输入转换为它的underlying_type,执行一些按位运算,然后将结果(使用static_cast)存储到目标。但是对于没有underlying_type 的整数类型,不应该进行这些转换。

c++ templates sfinae static-cast enum-class

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

使用 std::string 声明一个 std::bitset 数组

我目前正在尝试声明一个包含 17 个 std::bitset 的数组,每个 32 位长。我是这样做的:

std::bitset<32> mTestInstruction[17]
{
    std::string("01000000001000000000000000000001"),
    std::string("01000000011000000000000001100011"),
    std::string("01000000101000000000000000000001"),
    std::string("10100000000000000000000000001010"),
    std::string("00000000100000010000000010000010"),
    std::string("00000000110001010010000000000001"),
    std::string("01001000111001010000000000000000"),
    std::string("01000100001000110000000000000011"),
    std::string("01000000001000010000000000000001"),
    std::string("10000000000000000000000000000011"),
    std::string("00000000010000000000000000000001"),
    std::string("00000000111000000000000000000001"),
    std::string("00000000111001110000100000000001"),
    std::string("01000000010000100000000000000001"),
    std::string("01000100001000100000000000000010"),
    std::string("10000000000000000000000000001100"),
    std::string("11100000000000000000000000001000"),
};
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

error: could not convert 'std::__cxx11::basic_string<char>(((const char*)"01000000001000000000000000000001"), std::allocator<char>())' from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'std::bitset<32u>'

对于每个位串。

我不明白为什么会发生这种情况,因为根据 cpp 参考, std::string 是构造位集的有效方法。谁能指出如何解决这个问题?

c++ bitset c++11

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

编译器推断模板参数

template<typename T>
class A
{
    public:

    A(T &t)
    : t_(t){}

    T t_;
};


int main()
{
    int value;
    A<decltype(value)> a(value);
    // what I wish for : A a(value); 
    // which does not compile "missing template argument before 'a'"
}
Run Code Online (Sandbox Code Playgroud)

在 A (或其他地方)的声明中有没有办法提示编译器 T 应该自动解析为传递给构造函数的类型?

(理想情况下是 c++11,但很高兴听到不那么旧的版本)

c++ templates compilation decltype declval

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

当我们为类的对象分配整数值时,为什么会调用参数化构造函数?

代码是:

#include<iostream>
using namespace std;

class Integer
{
    int num;

    public:
        Integer()
        {
            num = 0;
            cout<<"1";
        }
        
        Integer(int arg)
        {
            cout<<"2";
            num = arg;
        }
        int getValue()
        {
            cout<<"3";
            return num;
        }

};

int main()
{
    Integer i;
    i = 10;  // calls parameterized constructor why??
    cout<<i.getValue();
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,语句i=10调用了参数化构造函数。你能解释一下吗。

c++ constructor assignment-operator parameterized-constructor

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