C ++如何将它拆分成向量并使用它 [英] C++ How do i split this into vector and use it
问题描述
可能重复:
使用C ++拆分字符串
使用C ++编程。
我的客户端发送一个值为
的字符串 string receiveClient =auth#user:pass;
如何拆分 receiveClient
'#'
和':'
>
我试图使用这个在线找到的函数
string& s,char delim)
{
vector string elems;
return(s,delim,elems);
}
code>:
向量x = split(& receiveClient,#);
但它返回下列
: server.cpp:在函数'int main()':
server.cpp:128:8:错误:在'x'之前缺少模板参数
server.cpp :128:8:error:expected';'before'x'
root @ ubuntu:/ home / baoky / csci222_assn2#g ++ server server.cpp
server.cpp:47:1:error:invalid使用template-name'std :: vector'没有参数列表
server.cpp:在函数'int main()':
server.cpp:128:8:error: 'x'
server.cpp:128:8:error:expected';'before'x'
$ b b
感谢您的帮助。非常感谢
这样的任务通常是最容易使用C ++中的流。这样的东西应该工作:
//小心,大脑编译代码!
#include< vector>
#include< string>
#include< sstream>
std :: vector< string> splitClientAuth(const std :: string& receiveClient)
{
//auth#user:pass
std :: istringstream iss(receiveClient);
std :: vector< std :: string>字符串;
strings.resize(3);
std :: getline(iss,strings [0],'#');
std :: getline(iss,strings [1],':');
std :: getline(iss,strings [2]); // default is'\\\
'
if(!iss&&!iss.eof())
throwDude,you veryly need an error handling strategy!
if(string [0] .empty()|| string [1] .empty()|| string [2] .empty())
throwWatcha gonna do now? ;
返回字符串;
}
注意:
-
这些是真的 纯文字密码 吗?
-
在
std :: vector< std :: string>
如果这是我的代码,我想要 数据结构来存储用户信息 ,并写入我写的东西。 -
判断你完全不理解你在你的问题中粘贴的代码(Martinho是对的,这太糟糕了,可以说它是否仍然可以被认为是C ++ ),并根据您的意见, 您似乎不太需要良好的C ++书 。
Possible Duplicate:
Splitting a string in C++
I am doing client server programming using C++.
My client sends a string with the value
string receiveClient = "auth#user:pass";
How do I split the receiveClient
variable by '#'
and ':'
as delimiters?
I have tried to use this function I found online
vector split (const string &s,char delim)
{
vector string elems;
return(s,delim,elems);
}
and I did this at main()
:
vector x = split(&receiveClient,"#");
But it return me the following
server.cpp: In function ‘int main()’:
server.cpp:128:8: error: missing template arguments before ‘x’
server.cpp:128:8: error: expected ‘;’ before ‘x’
root@ubuntu:/home/baoky/csci222_assn2# g++ server server.cpp
server.cpp:47:1: error: invalid use of template-name ‘std::vector’ without an argument list
server.cpp: In function ‘int main()’:
server.cpp:128:8: error: missing template arguments before ‘x’
server.cpp:128:8: error: expected ‘;’ before ‘x’
Thanks for all help. greatly appreciated
Such tasks are usually easiest done using streams in C++. Something like this should work:
// Beware, brain-compiled code ahead!
#include <vector>
#include <string>
#include <sstream>
std::vector<string> splitClientAuth(const std::string& receiveClient)
{
// "auth#user:pass"
std::istringstream iss(receiveClient);
std::vector<std::string> strings;
strings.resize(3);
std::getline(iss, strings[0], '#');
std::getline(iss, strings[1], ':');
std::getline(iss, strings[2]); // default is '\n'
if( !iss && !iss.eof() )
throw "Dude, you badly need an error handling strategy!";
if( string[0].empty() || string[1].empty() || string[2].empty() )
throw "Watcha gonna do now?";
return strings;
}
A few additional point worth noting:
Are those really plain-text passwords?
Having this in a
std::vector<std::string>
seems dubious to me. If that was my code, I'd want a data structure to store user information, and write what I find write into that.Judging from you totally failing to understand the code you pasted in your question (Martinho is right, that's so bad, it's arguable whether it can still be considered C++), and from your comments, you seem to be in bad need of a good C++ book.
这篇关于C ++如何将它拆分成向量并使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!