C++ 我的分割函数不工作,错误C2664,
所以这应该是拆分字符串并将其转换为列表:C++ 我的分割函数不工作,错误C2664,,c++,c++11,c2664,C++,C++11,C2664,所以这应该是拆分字符串并将其转换为列表: std::list strSplit(std::string::iterator begin,std::string::iterator end,char c){ 而(std::find(begin,end,c)!=end)*std::find(begin,end,c)='\0'; 返回标准::列表(开始、结束); } 但编译器抛出以下错误: Error: C2664 'std::basic_string<char,std::char_t
std::list strSplit(std::string::iterator begin,std::string::iterator end,char c){
而(std::find(begin,end,c)!=end)*std::find(begin,end,c)='\0';
返回标准::列表(开始、结束);
}
但编译器抛出以下错误:
Error: C2664
'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string
(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)':
cannot convert argument 1 from 'char' to 'std::initializer_list<_Elem>'
错误:C2664
'std::basic_string::basic_string
(const std::basic_string&)':
无法将参数1从“char”转换为“std::initializer\u list”
然后双击重定向到xmemory0行:881
...
template<class _Objty,
class... _Types>
static void construct(_Alloc&, _Objty * const _Ptr, _Types&&... _Args)
{ // construct _Objty(_Types...) at _Ptr
::new (const_cast<void *>(static_cast<const volatile void *>(_Ptr)))
_Objty(_STD forward<_Types>(_Args)...);
}
...
。。。
模板
静态void构造(_Alloc&,_Objty*const _Ptr,_Types&&…_Args)
{//在Ptr处构造对象(_类型…)
::新建(常量转换(静态转换(_Ptr)))
_对象(_stdforward(_Args)…);
}
...
您需要使用从std::find
返回的迭代器,并从中实际创建std::string
(以及begin
迭代器)。将c
替换为\0
只会使原始字符串中包含\0
,因此生成的std::list
中唯一的字符串也是这样
关于这个主题,您可能应该使用std::string::const_迭代器
,因为您可能不应该更改原始字符串
例如:
#include <iterator>
std::list<std::string> strSplit(std::string::const_iterator begin,
std::string::const_iterator end, char c) {
std::list<std::string> retval;
if(begin == end) return retval; // nothing to do here
while(true) {
auto fit = std::find(begin, end, c);
retval.emplace_back(begin, fit); // creates a string in-place using begin and fit
if(fit == end) return retval; // if end was found, return what we got
begin = std::next(fit); // else, go another round
}
}
#包括
std::list strSplit(std::string::const_迭代器begin,
std::string::const_迭代器end,char c){
std::列表检索;
if(begin==end)返回retval;//此处无需执行任何操作
while(true){
自动拟合=标准::查找(开始、结束、c);
retval.emplace_back(begin,fit);//使用begin和fit在适当的位置创建字符串
if(fit==end)return retval;//如果找到end,则返回我们得到的
begin=std::next(fit);//否则,再进行一轮
}
}
您的函数存在多个问题。除了不编译之外。它尝试将它试图复制到列表中的字符串进行变异,这是没有意义的。它还无缘无故地多次使用std::find
,而不是存储其结果并重用它。以下是我的版本:
std::list strSplit(std::string::const\u迭代器const begin,
std::string::const_迭代器const end,
字符常量(c){
std::list res;
如果(结束!=开始){
自动it=开始,prev=开始;
while((it=std::find(it,end,c))!=end){
res.emplace_back(上一个,it);
prev=++it;
}
重新安置(上,结束);
}
返回res;
}
您不能以这种方式从std::string
创建std::list
。您需要遍历字符串,找到感兴趣的子字符串并将它们放入列表中。std::list constructor#5()采用迭代器范围begin和end,所以这不是问题所在。问题是std::string::iterator遍历字符串中的字符,因此strSplit函数需要返回std::list,但这并不能解决您的问题,因为您希望将“\0”粘贴到字符串中会使其变成两个字符串,否则,您将得到一个包含“\0”字符的字符串。实际上,您需要创建两个单独的std::string对象并将它们放入列表中。您需要@JerryJeremiah之类的东西,为什么不添加一个包含所有文本和代码的答案?与我所看到的现有答案的一个主要区别是,如果begin==end
,它将创建一个std::list
,其中包含一个空字符串,但这可能正是OP想要的。当我开始编写代码时,没有答案。当我完成时,有两个。我的答案基本上是相同的,因此我认为,做出与现有答案相同的答案没有多大意义,但我不想浪费代码,所以我想我会分享它。