如何强制std :: stringstream operator >>读取整个字符串而不是在第一个空格处停止?
我有一个模板类,用于存储从文本文件中读取的值:
template <typename T>
class ValueContainer
{
protected:
T m_value;
public:
/* ... */
virtual void fromString(std::string & str)
{
std::stringstream ss;
ss << str;
ss >> m_value;
}
/* ... */
};
Run Code Online (Sandbox Code Playgroud)
我已经尝试设置/取消设置流标志,但它没有帮助.
澄清
该类是一个容器模板,可以自动转换为T类型.字符串只是模板的一个实例,它也必须支持其他类型.这就是为什么我想强制operator >>模仿std :: getline的行为.
我有一个std::vector<uint8_t>包含特定偏移的字符串.这是一个缩短的转储:
...
@128 00 00 00 00 00 00 00 00 73 6F 6D 65 74 68 69 33 ........somethin
@144 38 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ng..............
@160 00 00 00 00 00 00 00 00 31 2E 32 2E 33 00 00 00 ........1.2.3...
@176 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
...
Run Code Online (Sandbox Code Playgroud)
我试图在偏移136处提取数据并将其放入std::string: …
我正在使用只包含char [32]的结构写入二进制文件.我基本上需要通过对字符串数组执行各种计算并连接结果来格式化每个数据块.我试图将std :: string复制到没有空终止的char数组.我读到的越多,我就越困惑.如果我做:
struct block{
char data[32];
};
block blocks[2048];
std::string buffer;
buffer = "12345678123456781234567812345678";
strcpy(blocks[0].data, buffer.c_str());
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,因为使用c_str()添加空终止符使字符串长度为33.如果我从字符串中减去一个字符,它可以工作,但后来我有空终结符,我不想要.我可以成功地做到以下几点:
strcpy(blocks[0].data, "12345678123456781234567812345678");
Run Code Online (Sandbox Code Playgroud)
但我想先构造字符串,因为它通常涉及连接各种数组的不同字符串.例如,我可以用std :: string来做到这一点:
std::string buffer = stringArray1[0] + stringArray2[0];
strcpy(blocks[0].data, buffer.c_str());
Run Code Online (Sandbox Code Playgroud)
但后来我再次使用null终止符.我只想复制std :: string中的字符而没有空终止符.
我使用的是VC++ 6.0.
我想cvopy boost::array<char>到std::string.
boost::array<char, 1024> _buffer;
std::string data;
std::copy(_buffer.begin(), _buffer.begin()+bytes_transferred, data.begin());
Run Code Online (Sandbox Code Playgroud)
这是行不通的.所以我稍微改了一下.
char _buffer[1024];
std::string data;
std::copy(_buffer, _buffer+bytes_transferred, data.begin());
Run Code Online (Sandbox Code Playgroud)
第二个也没有工作.
对于你们中的一些人来说这可能是个简单的问题.但我想知道a std::string是否是一个容器.容器我的意思是容器,例如std::vector,std::list和std::deque.
由于std::basic_string<>接受除积分字符之外的其他类型,但也通过使用字符数组进行优化.我不清楚它属于哪个类别.
这将编译:
#include <string>
#include <iostream>
int main() {
std::basic_string<int> int_str;
int_str.push_back(14);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是通过添加这一行:
std::cout << int_str << std::endl;
Run Code Online (Sandbox Code Playgroud)
它不会.因此,通过这些事实,我可以得出结论,std :: basic_string不适用于除字符之外的其他类型.
这可能是一个奇怪的问题.我需要知道这个的原因是因为我正在研究一个框架而我仍然无法确定"字符串"会落在哪个类别中.
我正在尝试将字符串转换为数字.为此,我发现了以下方式:
#include <iostream>
#include <string>
template <typename T>
T stringToNumber(const std::string &s)
{
std::stringstream ss(s);
T result;
return ss >> result ? result : 0;
}
int main()
{
std::string a = "254";
int b = stringToNumber(a);
std::cout << b*2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
问题是我收到以下错误:
错误:没有匹配函数来调用'stringToNumber(std :: string&)'
任何人都可以告诉我为什么我会收到这样的错误以及如何修复它?
先感谢您.
我正在编写一个使用c套接字的c ++程序.我需要一个函数来接收我想要返回字符串的数据.我知道这不起作用:
std::string Communication::recv(int bytes) {
std::string output;
if (read(this->sock, output, bytes)<0) {
std::cerr << "Failed to read data from socket.\n";
}
return output;
}
Run Code Online (Sandbox Code Playgroud)
因为read()*函数采用char数组指针作为参数.在这里返回字符串的最佳方法是什么?我知道理论上我可以将数据读入char数组然后将其转换为字符串,但这对我来说似乎很浪费.有没有更好的办法?
*read()如果有更合适的选择,我实际上并不介意使用其他东西
以下是pastebin上应该在一周内到期的所有代码.如果我当时没有答案,我会重新发布:http://pastebin.com/HkTDzmSt
[UPDATE]
我也试过使用&output[0]但得到的输出包含以下内容:
jello!
[insert a billion bell characters here]
Run Code Online (Sandbox Code Playgroud)
"爽!" 是将数据发送回套接字.
如何将a转换CFURLRef为C++ std::string?
我还可以从转换CFURLRef的CFStringRef方式:
CFStringRef CFURLGetString ( CFURLRef anURL );
Run Code Online (Sandbox Code Playgroud)
但现在我有同样的问题.我怎样才能转换CFStringRef为std::string?
考虑以下代码:
#include <utility>
#include <string>
int bar() {
std::pair<int, std::string> p {
123, "Hey... no small-string optimization for me please!" };
return p.first;
}
Run Code Online (Sandbox Code Playgroud)
(简化感谢@ Jarod42 :-) ...)
我希望该功能可以简单地实现:
bar():
mov eax, 123
ret
Run Code Online (Sandbox Code Playgroud)
但是,实现调用operator new(),std::string用我的文字构造 an ,然后调用operator delete(). 至少 - 这就是 gcc 9 和 clang 9 所做的(GodBolt)。这是铛的输出:
bar(): # @bar()
push rbx
sub rsp, 48
mov dword ptr [rsp + 8], 123
lea rax, [rsp + 32]
mov qword ptr [rsp …Run Code Online (Sandbox Code Playgroud) 我尝试了这些代码来比较std::copyandstd::string的构造函数。
#include <chrono>
#include <iostream>
#include <vector>
void construct_test() {
std::vector<uint8_t> raw_data;
for (int i = 0; i < 1000 * 1024; i++) {
raw_data.push_back(i % 256);
}
auto start = std::chrono::high_resolution_clock::now();
std::string target_data;
target_data = std::string(raw_data.begin(), raw_data.end());
auto finish = std::chrono::high_resolution_clock::now();
std::cout << "construct: " << std::chrono::duration_cast<std::chrono::microseconds>(finish -
start)
.count()
<< "us" << std::endl;
}
void copy_test() {
std::vector<uint8_t> raw_data;
for (int i = 0; i < 1000 * 1024; i++) {
raw_data.push_back(i % 256); …Run Code Online (Sandbox Code Playgroud)