Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Stringstream缓冲区赢了';t在Objective-C+中为空+;_C++_Objective C_Objective C++ - Fatal编程技术网

C++ Stringstream缓冲区赢了';t在Objective-C+中为空+;

C++ Stringstream缓冲区赢了';t在Objective-C+中为空+;,c++,objective-c,objective-c++,C++,Objective C,Objective C++,我想在Objective-C++中使用std::stringstream,但在这一行之后缓冲区不会为空: ss >> username >> name >> surname >> Id >> email; 我的代码如下所示: std::shared_ptr<Self> string_to_self(const std::string & info){ std::stringstream ss; ss

我想在Objective-C++中使用std::stringstream,但在这一行之后缓冲区不会为空:

ss >> username >> name >> surname >> Id >> email;
我的代码如下所示:

std::shared_ptr<Self> string_to_self(const std::string & info){
    std::stringstream ss;
    ss.str(info);
    std::string username{""};
    std::string name{""};
    std::string surname{""};
    int Id{0};
    std::string email{""};

    ss >> username >> name >> surname >> Id >> email;

    //ss.str() is the same as 'info'.
std::shared_ptr string_to_self(const std::string&info){
std::stringstream-ss;
ss.str(信息);
std::字符串用户名{“”};
std::字符串名{“”};
std::字符串姓氏{“”};
int Id{0};
std::字符串电子邮件{“};
ss>>用户名>>姓名>>姓氏>>身份证>>电子邮件;
//str()与“info”相同。
<> PosiSoots在Objto-C++中与C++中的工作方式是否相同?
这还有其他更好的方法吗?

它按预期工作。stringstream没有清空,它只是调整操纵器的位置

所以代码如下:

ss.str()
以字符串形式返回stringstream内容的副本(无论读取指针在字符串中的偏移量如何),并且不更改stringstream的内容

如果要将流字符串设置为剩余内容,则需要进行一些人为操作:

ss.str(ss.tellg() == -1 ? "" : ss.str().substr(ss.tellg()));

这将确定寻道位置,如果它是
-1
,则清空内容,否则将其设置为寻道位置后面的子字符串。

其工作正常,您应该自己清除它。您应该调用清除已设置的任何标志,然后使用新值初始化它

第一种方法可以避免调用
std::string

ss.clear();
ss.str(std::string());
或者你可以简单地

ss.clear();
ss.str("");

大多数现代编译器都避免调用
constchar
constructor

在这里,或许
std::istream\u迭代器对您更有用

std::shared_ptr<Self> string_to_self(const std::string & info){
    std::stringstream ss(info);
    std::istream_iterator<std::string> foo(ss);
    const std::string username = *foo++;
    const std::string name = *foo++;
    const std::string surname = *foo++;
    const int Id = std::stoi(*foo++);
    const std::string email = *foo++;

    if(foo == std::istream_iterator<std::string>()){
        //empty
    }else{
        //not empty
    }
std::shared_ptr string_to_self(const std::string&info){
std::stringstream ss(信息);
std::istream_迭代器foo(ss);
const std::string username=*foo++;
const std::string name=*foo++;
常量std::字符串姓氏=*foo++;
const int Id=std::stoi(*foo++);
const std::string email=*foo++;
if(foo==std::istream\u迭代器()){
//空的
}否则{
//不空
}