拆分和分析字符串,然后在C 中转换为char*
split and parse string and convert into char* in c++
我在下面有几个问题。我仅从stackoverflow中获取了此代码,并尝试理解。
拆分代码的工作方式是特定的:我无法理解以下代码
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
我添加了删除空间的代码,但它给了我编译错误。我已经采用了代码stackoverflow,无法弄清楚错误
split.cpp:34:88: error: no matching function for call to
‘remove_if(std::vector<std::basic_string<char> >::iterator,
std::vector<std::basic_string<char> >::iterator, <unresolved
overloaded function type>)’ split.cpp:34:88: note: candidate is: In
file included from
/linux/depot/gcc-4.7.0/bin/../lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from split.cpp:5: /linux/depot/gcc-4.7.0/bin/../lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:1140:5:
note: template<class _FIter, class _Predicate> _FIter
std::remove_if(_FIter, _FIter, _Predicate)
/linux/depot/gcc-4.7.0/bin/../lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:1140:5:
note: template argument deduction/substitution
failed:split.cpp:34:88: note: couldn't deduce template parameter
‘_Predicate’
如何将vector<string>
转换为char*
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
int main()
{
std::vector<std::string> f_data;
f_data.push_back("A= 99.58%");
f_data.push_back("B= 78%");
f_data.push_back("C= 90%");
vector<string>::iterator t_data;
for(t_data = f_data.begin(); t_data != f_data.end(); t_data++)
{
vector<string> temp_data = split(*t_data, '=');
//temp_data.erase(std::remove_if(temp_data.begin(), temp_data.end(), std::isspace), temp_data.end());
vector<string>::iterator data;
for(data = temp_data.begin(); data != temp_data.end(); data++)
{
cout<<*data;
}
}
return 0;
}
为什么您返回elems vector?您通过引用将其传递,您不必退还。
void split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
当然,这看起来更好(请注意,它是按值返回的):
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
至于如何将vector<string>
转换为char*
,将矢量中的字符串连接起来,然后使用c_str()
。
有两个std :: isspace(一个in&lt; cctype>,另一个in in&lt; locale>)。另外,您应该删除空格,而不是向量中的字符串:
inline bool is_space(char c) { return std::isspace(c); }
...
for(std::vector<std::string>::iterator t = temp_data.begin(); t != temp_data.end(); ++t) {
std::string& s = *t;
std::remove_if(s.begin(), s.end(), is_space);
}
...
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 使用char类型将decimal转换为string,将string转换为decimal
- 如何在对<char>C++程序进行逆向工程的同时将 std::basic_string 转换为 Rust 可读值?
- 将函数参数"const char*"转换为"std::string_view"是
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 如何在不强制转换每个参数的情况下删除初始值设定项列表中从 int 到 char 的缩小转换?
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 如何使用多次 memcpy 转换回 std::vector<unsigned char>?
- 将"uint8_t"(从套接字读取)隐式转换为"char"安全吗
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 将传入的网络"char*"数据转换为"uint8_t"并返回的安全方法是什么?
- 转换char*时的值不同
- Boost Python 不会自动转换 char* 数据成员
- 除了转换char*之外,还有更好的方法来获得一个变量值吗?
- 如何跨平台转换char*到long long(64位)
- 用atof转换char数组中的浮点数
- C转换char[]为时间戳;