istringstream的操作
今天在stackoverflow上看到这么个问题,写完之后看了看别人的提交的答案,感觉自己的答案虽然能得出正确结果但是有点啰嗦,对于c++还是没有熟练,没有想起有istringstream,而且提问的老外写的程序bug实在是太多了,while循环啊,分号啊都不规范,不过那个return挺有意思,嘿嘿第一次见(可能是写的代码和看的代码太少了吧 ,嘿嘿 ,见笑,我也是一菜),于是我放弃了我原来的答案,修改了一下别人的答案,那个人的答案也不对,不能运行啊,所以说我自己改动了一下.
今儿记录一下,省的以后忘..
老外的提问:
struct client
{
string PhoneNumber;
string FirstName;
string LastName;
string Age;
};
int main()
{
string data = getClientDatabase(); vector <client> clients; parse_string(data, clients);
return ;
} string getClientDatabase()
{
return
"(844)615-4504 Sofia Ross 57 \n"
"(822)516-8895 Jenna Doh 30 \n"
"(822)896-5453 Emily Saks 43 \n" } void parse_string(string data, vector <client> &clients)
{
string temp;
string temp1;
string temp2;
string temp3; int i = ;
int j = ;
int k = ;
int l = ; while (i < data.length())
{
if (data.at(i) != ' ')
{
temp.push_back(data.at(i));
j++;
}
else
{
clients.at(i).PhoneNumber = temp;
} }
if (data.at(j) != ' ')
{
temp1.push_back(data.at(j));
k++;
}
else
{
clients.at(i).FirstName = temp1;
} if (data.at(k) != ' ')
{
temp2.push_back(data.at(k));
l++;
}
else
{
clients.at(i).LastName = temp2;
} if (data.at(l) != ' ')
{
temp3.push_back(data.at(l)); }
else
{
clients.at(i).Age = temp3;
}
i++; }
我的回答:
void parse_string(string data, vector <client> & clients)
{
struct client tempS;
istringstream iss(data);
for (size_t i=; iss >> tempS.PhoneNumber; ++i)
{
iss >> tempS.FirstN >> tempS.LastN >> tempS.Age;
clients.push_back(tempS);
}
}
//test code vector <client>::iterator it = clients.begin();
for(; it != clients.end(); ++it )
{
cout << it->PhoneNumber << " "
<< it->FirstN << " "
<< it->LastN << " "
<< it->Age << endl;
}
istringstream的操作的更多相关文章
- istringstream、ostringstream、stringstream 类简介
本文系转载,原文链接:http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html ,如有侵权,请联系我:534624117@qq.co ...
- 初识 istringstream、ostringstream、stringstream 运用
今天编程练习时遇到了istringstream的用法,感觉很实用.后面附题目! C++的输入输出分为三种: (1)基于控制台的I/O (2)基于文件的I/O (3)基于字符串的I/O 1.头文件 # ...
- c++数组、字符串操作
一.数组操作 1.数组初始化1-1一维数组初始化:标准方式一: int value[100]; // value[i]的值不定,没有初始化标准方式二: int value[100] = {1,2}; ...
- istringstream、ostringstream、stringstream 类介绍 .
istringstream.ostringstream.stringstream 类介绍 . 转自:http://www.cnblogs.com/gamesky/archive/2013/01/09/ ...
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=2866&rd=5853 主要是要对字符串的操作要熟悉,熟 ...
- C++中使用stringstream进行类型转换操作
stringstream包括istringstream和ostringstream,提供读写string的功能,使用时需包含stream文件.4个操作:1. stringstream strm; 创建 ...
- istringstream和ostringstream的使用方法
写程序用到istringstream和ostringstream,看了别人的博文,借鉴~~~~~~. iostream 标准库支持内存中的输入/输出,只要将流与存储在程序内存中的 string 对象捆 ...
- C++ IO操作API及注意事项(包含一个日志类的实现)
C++是一个抽象程度比C高很多的语言,在使用C++时,编译器做了很多工作,如果我们不对C++的某些特性的实现机制进行了解,那么编程时也许会有很多疑惑,我们也许知道怎样做才是正确的,但不知道为什么要这样 ...
- C++对txt文本进行读写操作
输入输出,是每个程序员的基本功,尤其是对文本的输入和输出.最近,自己在这方面做了一些总结,不是很全面,希望在以后学习和工作的过程中慢慢补充,积累点点滴滴.P.S. 今天天气不错,雾霾散了,天空晴朗,惠 ...
随机推荐
- PHP cookie
PHP Cookie cookie 常用于识别用户. Cookie 是什么? cookie 常用于识别用户.cookie 是一种服务器留在用户计算机上的小文件.每当同一台计算机通过浏览器请求页面时,这 ...
- C# *= 运算顺序
a *= a + b *c; 不管等号右边有没有括号,总是先算右边: 即等价于 a = a *(a + b*c); using System; using System.Collections.Gen ...
- 《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX
<CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...
- Go - 函数/方法 的 变参
变参 本质上就是一个切片.只能接收一个或多个同类型参数,且 必须放在参数列表的 尾部. func test(s string, a ...int) { fmt.Printf("%T, %v\ ...
- MFC在关闭第二个窗口时关闭主对话框
AfxGetApp()->m_pMainWnd->SendMessage(WM_CLOSE);//关闭主对话框
- jquery版小型婚礼(可动态添加祝福语)
前两天在网上不小心看到“js许愿墙”这几个字,我的神经就全部被调动了.然后就开始我的百度生涯,一直寻觅许愿墙背景图片和便利贴图片,觅了好久……一直没找到满意的……无意间看到祝福语和一些卡通婚礼图片.最 ...
- java11
1:对象数组(掌握) (1)数组既可以存储基本数据类型,也可以存储引用类型.它存储引用类型的时候的数组就叫对象数组. (2)案例: 用数组存储5个学生对象,并遍历数组. 2:集合(Collection ...
- 使用curl进行https请求
简单示例: /** * @param $url * @return array * 进行https请求,并且遇到location进行跳转 */ function https($url){ $resul ...
- js计算地球两个经纬度之间的距离
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- eclipse 粘贴字符串自动添加转义符
eclipse -> Window -> Preferences -> Java -> Editor -> Typing -> 勾选{Escape text whe ...