小编Tom*_*Tom的帖子

通过 C++ 中的重载构造函数初始化未知类型的变量

主要来自 Python 背景,我在使用 C++ 类型时有些挣扎。

我试图通过将不同类型作为参数的几个重载构造函数之一来初始化一个类变量。我已经读过使用auto关键字可以用于变量的自动声明,但是在我的情况下,它不会被初始化,直到选择了构造函数。然而,编译器对不初始化不满意value

class Token {
public:

    auto value;

    Token(int ivalue) {
        value = ivalue;
    }
    Token(float fvalue) {
        value = fvalue;
    }
    Token(std::string svalue) {
        value = svalue;
    }

    void printValue() {
        std::cout << "The token value is: " << value << std::endl;
    }
};
Run Code Online (Sandbox Code Playgroud)

在 python 中,这可能如下所示:

class Token():
        def __init__(self, value):
             self.value = value

        def printValue(self):
             print("The token value is: %s" % self.value)
Run Code Online (Sandbox Code Playgroud)

auto在这种情况下使用关键字的正确方法是什么?我应该完全使用不同的方法吗?

c++ constructor overloading initialization auto

22
推荐指数
3
解决办法
1660
查看次数

如何将 xarray 转换为 std::vector?

该文档非常清楚地说明了如何使 a 适应std::vector张量对象。 https://xtensor.readthedocs.io/en/latest/adaptor.html

std::vector<double> v = {1., 2., 3., 4., 5., 6. };
std::vector<std::size_t> shape = { 2, 3 };
auto a1 = xt::adapt(v, shape);
Run Code Online (Sandbox Code Playgroud)

但你怎么能反过来呢?

xt::xarray<double> a2 = { { 1., 2., 3.} };
std::vector<double> a2vector = ?;
Run Code Online (Sandbox Code Playgroud)

stdvector xtensor

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

Boost gzip如何将压缩字符串输出为文本

我在这里使用 boost gzip 示例代码。我正在尝试压缩一个简单的字符串测试,并期望压缩字符串H4sIAAAAAAAACitJLS4BAAx+f9gEAAAA如该在线压缩器所示

static std::string compress(const std::string& data)
{
    namespace bio = boost::iostreams;
    std::stringstream compressed;
    std::stringstream origin(data);

    bio::filtering_streambuf<bio::input> out;
    out.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_compression)));
    out.push(origin);
    
    bio::copy(out, compressed);
    return compressed.str();
}

int main(int argc, char* argv[]){
    std::cout << compress("text") << std::endl;
    // prints out garabage

    return 0;
}

Run Code Online (Sandbox Code Playgroud)

然而,当我打印出转换结果时,我得到了像+I-这样的垃圾值。〜

我知道这是一个有效的转换,因为解压值返回正确的字符串。但是我需要字符串的格式是人类可读的,即H4sIAAAAAAAACitJLS4BAAx+f9gEAAAA

如何修改代码以输出人类可读的文本?

谢谢

动机

垃圾格式与我将通过其发送压缩文本的 JSON 库不兼容。

c++ compression boost gzip

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

以向量为键的 C++ 映射。如何使它如此矢量顺序无关紧要?

我有一个以整数向量为键的地图。我用值的关键向量初始化地图{1, 2, 3}

typedef std::map<std::vector<int>, std::string> VectorMap;
VectorMap vectorMap;
vectorMap[std::vector<int>{1, 2, 3}] = "test";
Run Code Online (Sandbox Code Playgroud)

然后我使用count方法来显示是否可以通过向量{1, 2, 3}作为关键字找到VectorMap中的条目。

std::cout << "count: " << vectorMap.count(std::vector<int>{1, 2, 3}) << std::endl;
Run Code Online (Sandbox Code Playgroud)

这将返回正确的计数。

数:1

但是我想这样做,所以向量中整数的顺序无关紧要。所以我尝试与上述相同但矢量内容翻转即{3, 2, 1}

std::cout << "count: " << vectorMap.count(std::vector<int>{3, 2, 1}) << std::endl;
Run Code Online (Sandbox Code Playgroud)

这将返回 0 的计数。

计数:0

我想让矢量比较与内容的顺序无关,只要内容相同。

{1, 2, 3} count: 1
{3, 2, 1} count: 1
{1, 2} count: 0
{1, 2, 3, 4} count : 0
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?我应该完全使用不同的容器而不是 std::vector 吗?

c++ containers stdmap stdvector unordered

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

Bison 中的 std::shared_ptr 导致成员错误

我正在尝试通过使用来提高野牛的内存效率std::shared_ptr。我不想使用原始指针。我使用节点系统作为解析树,所以我定义YYTYPEstd::shared_ptr<Node>。用一些简单的语法运行它后,我得到编译错误:

C2039 'blockNode':不是 'std::shared_ptr' 的成员

我觉得这很奇怪,因为在 C++ 中运行的等效代码工作得很好

std::shared_ptr<Node> test = std::make_shared<BlockNode>();

我缺少什么?

需要

%code requires {
    typedef void* yyscan_t;
    #include "node.h"
    #include <memory>
    #define YYSTYPE std::shared_ptr<Node>
}
Run Code Online (Sandbox Code Playgroud)

联盟

%union {
    std::shared_ptr<BlockNode> blockNode;
    std::shared_ptr<TestNode> testNode;
}

%type <blockNode> input
%type <testNode> expr
Run Code Online (Sandbox Code Playgroud)

语法

%start program

%%

program : input { *result = $1; } // <- error here
        ;

input: '\n'      { $$ = std::make_shared<BlockNode>();} // <- error here
     ;

%%

Run Code Online (Sandbox Code Playgroud)

节点.h

class Node …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers bison flex-lexer

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

为什么传递带有 const 引用成员的结构会导致 C2280 错误?

我遇到了一个问题,而试图分配structconst&成员的地图。

struct test {
    const int& number;

    test(const int& cnumber) : number(cnumber) {}

    test(const test&) = default;
    test& operator=(const test&) = default;

};
Run Code Online (Sandbox Code Playgroud)
int main () {

    std::map<std::string, test> testmap;

    testmap["asd"] = test(2);

}

Run Code Online (Sandbox Code Playgroud)

运行此代码会导致错误 C2280 'test &test::operator =(const test &)': attempting to reference a deleted function

谁能向我解释这里的问题是什么?这是一个最小的可重现示例。在我的实际项目中,数据要大得多,因此按值传递是不明智的。

c++ struct reference constants

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

C++ 如何将 std::function 与不同返回类型的重载运算符一起使用

我正在尝试将标准std::function与自定义重载运算符一起使用。但是std::logical_and,在这种情况下Test,将string参数应用于我的班级是行不通的。

class Test {
public:
    std::string value;

    Test(std::string cvalue) : value(cvalue) {}

    std::string operator&& (const std::string& rhs) const {
        if (rhs == value) {
            return "true";
        }
        return "false";
    }
};

int main() {
    Test test("hey");
    std::string out = std::logical_and<std::string>()(test, "hey");
    std::cout << out << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

实现这一目标的正确方法是什么?我的预期输出是"true"

c++ operator-overloading

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