标签: stdstring

Google Protocol Buffer 序列化字符串可以包含嵌入的 NULL 字符吗?

我正在使用 Google Protocol Buffer 进行消息序列化。这是我的示例原型文件内容。

package MessageParam;

message Sample
{
    message WordRec
    {
        optional uint64 id = 1; 
        optional string word = 2;
        optional double value = 3;
    }
    message WordSequence
    {
        repeated WordRec WordSeq = 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试用 C++ 序列化消息,如下所示

MessageParam::Sample::WordSequence wordseq;
for(int i =0;i<10;i++)
{
    AddRecords(wordseq.add_wordseq());
}
std::string str = wordseq.SerializeAsString();
Run Code Online (Sandbox Code Playgroud)

执行上述语句后,str的大小为430。它嵌入了空字符。当我尝试将此 str 分配给 std::wstring 时,std::wstring 在找到第一个空字符时终止。

void AddRecords(MessageParam::Sample::WordRec* wordrec)
{
    int id;
    cin>>id;
    wordrec->set_id(id);
    getline(cin, *wordrec->mutable_word());
    long value;
    cin>>value;
    wordrec->set_value(value);
}
Run Code Online (Sandbox Code Playgroud)

wordseq.DebugString() 的值为 WordSeq { id: …

c++ null serialization stdstring protocol-buffers

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

在 gdb 中调用 std::~basic_string()

根据/sf/answers/791825051/中的@EvanED ,我创建了一个 gdb 命令newstr来创建一个新的 std::string 并将其放入 gdb 便利变量中:

define newstr
set ($arg0)=(std::string*)malloc(sizeof(std::string))
call ($arg0)->basic_string()
# 'assign' returns *this; casting return to void avoids printing of the struct.
call (void)( ($arg0)->assign($arg1) )
end
Run Code Online (Sandbox Code Playgroud)

效果很好:

(gdb) newstr $foo "hello world"
(gdb) p $foo->c_str()
$57 = 0xb22e388 "hello world"
Run Code Online (Sandbox Code Playgroud)

我newstr在其他自定义 gdb 命令中使用,因此为了整洁我还创建了delstr:

define delstr
call ($arg0)->~basic_string($arg0)
call free($arg0)
set ($arg0)=(void*)0
end
Run Code Online (Sandbox Code Playgroud)

它可以工作,但是析构函数调用会产生一条恼人的消息:

(gdb) delstr $foo
warning: Using non-standard conversion to match method std::string::~basic_string to supplied …
Run Code Online (Sandbox Code Playgroud)

gdb destructor type-conversion stdstring c++11

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

检查 C++ 中输入字符串是否有前导或尾随空格?

我正在尝试验证C++11 中的单行输入字符串,看看它是否包含任何前导/尾随空格。我的代码现在看起来像这样:

bool is_valid(const std::string& s) {
    auto start = s.begin();
    auto end = s.end();
    if (std::isspace(*start) || std::isspace(*end)) {
        return false;
    } 
    return true;
}

int main() {
    std::string name{};
    std::getline(std::cin, name);
    if (!is_valid(name)) {
        std::cout << "Invalid!";
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但现在程序只能检测前导空格。例如,因为 John它会打印Invalid!,但因为Mary 它会将其分类为有效输入,但事实并非如此。有谁知道我的程序出了什么问题?

c++ iterator stdstring c++11

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

如何将多个字符串相加,例如“123”+“456”?

怎么实现这样的操作,Visual Studio总是告诉我这是错误的。
错误的代码是C2110和E2140。
有人可以帮忙吗?

std::string a = "2323" + "22323" + "232332";
Run Code Online (Sandbox Code Playgroud)

c++ algorithm string-concatenation stdstring string-literals

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

默认构造的 std::string c_str() 值

std::string s1;\nstd::string s2;\nassert(strlen(s1.c_str()) == 0);\nassert(s1.c_str() == s2.c_str());\n
Run Code Online (Sandbox Code Playgroud)\n

这两个断言总是正确的吗?

\n

我使用C++11,并且我检查了标准,\xc2\xa721.4.2中的表63说:

\n
\n

data() 一个可复制的非空指针,可以添加 0

\n

大小() 0

\n

capacity() 一个未指定的值

\n
\n

我认为c_str()与 相同data()。但我对这个定义有一些疑问。

\n
    \n
  1. 是否“可以添加 0”==“必须且始终添加 0”?

    \n
  2. \n
  3. 所有默认构造的 std::string 是否共享相同的底层缓冲区?

    \n
  4. \n
\n

我在gcc上测试,这两个断言是正确的。我想知道这些对于所有编译器来说总是正确的吗?

\n

c++ stdstring

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

如何将 std::string 打包为 std::tuple&lt;Ts...&gt;

我有一个像这样的参数包

<int, long, string, double>
Run Code Online (Sandbox Code Playgroud)

和一个像这样的字符串

"100 1000 hello 1.0001"
Run Code Online (Sandbox Code Playgroud)

如何解析这些数据并将它们打包到 std::tuple<int, long, string, double>

c++ templates stdstring stdtuple c++20

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

将 CRLF(回车、换行)连接到 std::string 的规则

下面的代码输出

rm-data;
Content-Disposition: form-data;
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/z/h5z89qv6f

#include <iostream>
#include <string>

int main()
{
    std::string a = "Content-Disposition: form-data;" + '\r' + '\n';
    std::cout << a << std::endl;

    std::string b = "Content-Disposition: " + std::string("form-data;") + '\r' + '\n';
    std::cout << b << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我期望a有一个 CRLF 连接,但看起来它通过添加\r (13) + \n (10) = 23文本内起始偏移量的 ASCII 值来进行指针算术。

但是,对于b,CRLF 已正确连接。

也许更好的方法是

std::string a = "Content-Disposition: form-data;" + std::string("\r\n");
Run Code Online (Sandbox Code Playgroud)

或者

std::string a = "Content-Disposition: form-data;\r\n";
Run Code Online (Sandbox Code Playgroud)

问题

CRLF 之前的数据类型是否决定串联规则?

c++ stdstring string-literals

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

std :: string :: operator []的结果地址是否指向可写的,以空值终止的缓冲区?

我正在修改一个接受const char*并使用函数ProcessString的函数.ProcessString是一个函数,它期望以null结尾的字符缓冲区作为char*.缓冲区中的字符可能会也可能不会被修改,如下面的函数签名所定义.为了"弥合差距",我使用临时的std :: string:

void ProcessString( char* str );

void SomeFunction( const char* str )
{
  string temp(str);
  ProcessString( &temp[0] );
}
Run Code Online (Sandbox Code Playgroud)

我的主要问题是关于std :: string :: operator []的保证以及上面&temp [0]返回的地址是否是一个可用的,以null结尾的缓冲区作为char*.其次,其次,还有更好的方法吗?

我正在使用C++ 03.

c++ stl operators stdstring c++03

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

如何在C++中将LPOLESTR转换为char*

当我尝试使用CDynamicStringAccessor从我的存储过程中检索"列名"然后写入我的csv文件的第一行时,我要处理从LPOLESTR到char*OR std的转换: :串.

经过一些研究,我终于找到了最简单的方法.我会尽快发布答案,希望这能帮助那些有同样问题的人.

c++ string stdstring

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

std::string addition to char*

I'm trying to understand how addition of std::string to char* works. This code is compiling and working just as expected:

#include <string>
#include <cstdio>

void func (const char* str) {
  printf("%s\n", str);
}

int main () {
  char arr[] = {'a','b','c',0};
  char *str = arr;
  func((str + std::string("xyz")).c_str()); // THIS LINE
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

But I do not understand what constructors/methods is calling and in what order for this to work. This is addition of std::string to char* which gives …

c++ stdstring

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