Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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+中从中检索每个值+; 我需要在C++中分割字符串> ./>代码>_C++_Split_Stdstring - Fatal编程技术网

在点上拆分字符串并在C+中从中检索每个值+; 我需要在C++中分割字符串> ./>代码>

在点上拆分字符串并在C+中从中检索每个值+; 我需要在C++中分割字符串> ./>代码>,c++,split,stdstring,C++,Split,Stdstring,下面是我的字符串- @event.hello.dc1 现在我需要在上面的字符串上拆分,并从中检索@event,然后将@event传递给下面的方法- bool upsert(常量字符*键) 以下是我从以下网站读到的代码- 感谢您的帮助。您可以使用: 您可以使用strtok函数: 您可以通过执行以下操作来使用: strtok(sentence.c_str(), "."); 首先,您可以更改流的空间。方法是在新的std::locale中替换std::ctype方面,然后将新创建的std::loca

下面是我的字符串-

@event.hello.dc1

现在我需要在上面的字符串上拆分
,并从中检索@event,然后将
@event
传递给下面的方法-

bool upsert(常量字符*键)

以下是我从以下网站读到的代码-

感谢您的帮助。

您可以使用:


您可以使用strtok函数: 您可以通过执行以下操作来使用:

 strtok(sentence.c_str(), ".");
首先,您可以更改流的空间。方法是在新的
std::locale
中替换
std::ctype
方面,然后将新创建的
std::locale
插入流中。然而,这种方法有点涉及到手头的任务。事实上,要提取由
分隔的字符串的第一个组件,我甚至不会创建流:

std::string first_component(std::string const& value) {
    std::string::size_type pos = value.find('.');
    return pos == value.npos? value: value.substr(0, pos);
}

创建一个ctype方面,如下所示:

#include <locale>
#include <vector>

struct dot_reader: std::ctype<char> {
    dot_reader(): std::ctype<char>(get_table()) {}
    static std::ctype_base::mask const* get_table() {
        static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());

        rc['.'] = std::ctype_base::space;
        rc['\n'] = std::ctype_base::space; // probably still want \n as a separator?
        return &rc[0];
    }
};
#包括
#包括
结构点_读取器:std::ctype{
dot_reader():std::ctype(get_table()){}
静态std::ctype_base::mask const*get_table(){
静态std::vector rc(表大小,std::ctype_base::mask());
rc['.]=std::ctype_base::space;
rc['\n']=std::ctype_base::space;//可能仍然需要\n作为分隔符?
返回&rc[0];
}
};
然后在流中嵌入一个实例,并读取字符串:

istringstream iss(sentence);

iss.imbue(locale(locale(), new dot_reader())); // Added this

copy(istream_iterator<string>(iss), 
     istream_iterator<string>(), 
     ostream_iterator<string>(cout, "\n"));
istringstream-iss(句子);
iss.imbue(locale(locale(),new dot_reader());//加上这个
复制(istream_迭代器(iss),
istream_迭代器(),
ostream_迭代器(cout,“\n”);

您应该提到,
strtok
覆盖您传入的字符串。您不能将
c_str()
的结果(即
const char*
)传递给
strtok
,后者需要一个非
const
 strtok(sentence.c_str(), ".");
std::string first_component(std::string const& value) {
    std::string::size_type pos = value.find('.');
    return pos == value.npos? value: value.substr(0, pos);
}
#include <locale>
#include <vector>

struct dot_reader: std::ctype<char> {
    dot_reader(): std::ctype<char>(get_table()) {}
    static std::ctype_base::mask const* get_table() {
        static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());

        rc['.'] = std::ctype_base::space;
        rc['\n'] = std::ctype_base::space; // probably still want \n as a separator?
        return &rc[0];
    }
};
istringstream iss(sentence);

iss.imbue(locale(locale(), new dot_reader())); // Added this

copy(istream_iterator<string>(iss), 
     istream_iterator<string>(), 
     ostream_iterator<string>(cout, "\n"));