我正在使用 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: …
根据/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) 我正在尝试验证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 它会将其分类为有效输入,但事实并非如此。有谁知道我的程序出了什么问题?
怎么实现这样的操作,Visual Studio总是告诉我这是错误的。
错误的代码是C2110和E2140。
有人可以帮忙吗?
std::string a = "2323" + "22323" + "232332";
Run Code Online (Sandbox Code Playgroud) c++ algorithm string-concatenation stdstring string-literals
std::string s1;\nstd::string s2;\nassert(strlen(s1.c_str()) == 0);\nassert(s1.c_str() == s2.c_str());\nRun Code Online (Sandbox Code Playgroud)\n这两个断言总是正确的吗?
\n我使用C++11,并且我检查了标准,\xc2\xa721.4.2中的表63说:
\n\n\ndata() 一个可复制的非空指针,可以添加 0
\n大小() 0
\ncapacity() 一个未指定的值
\n
我认为c_str()与 相同data()。但我对这个定义有一些疑问。
是否“可以添加 0”==“必须且始终添加 0”?
\n所有默认构造的 std::string 是否共享相同的底层缓冲区?
\n我在gcc上测试,这两个断言是正确的。我想知道这些对于所有编译器来说总是正确的吗?
\n我有一个像这样的参数包
<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>
下面的代码输出
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 之前的数据类型是否决定串联规则?
我正在修改一个接受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.
当我尝试使用CDynamicStringAccessor从我的存储过程中检索"列名"然后写入我的csv文件的第一行时,我要处理从LPOLESTR到char*OR std的转换: :串.
经过一些研究,我终于找到了最简单的方法.我会尽快发布答案,希望这能帮助那些有同样问题的人.
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 …