主要来自 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在这种情况下使用关键字的正确方法是什么?我应该完全使用不同的方法吗?
该文档非常清楚地说明了如何使 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) 我在这里使用 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 库不兼容。
我有一个以整数向量为键的地图。我用值的关键向量初始化地图{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 吗?
我正在尝试通过使用来提高野牛的内存效率std::shared_ptr。我不想使用原始指针。我使用节点系统作为解析树,所以我定义YYTYPE为std::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) 我遇到了一个问题,而试图分配struct与const&成员的地图。
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
谁能向我解释这里的问题是什么?这是一个最小的可重现示例。在我的实际项目中,数据要大得多,因此按值传递是不明智的。
我正在尝试将标准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++ ×6
stdvector ×2
auto ×1
bison ×1
boost ×1
compression ×1
constants ×1
constructor ×1
containers ×1
flex-lexer ×1
gzip ×1
overloading ×1
reference ×1
stdmap ×1
struct ×1
unordered ×1
xtensor ×1