C_and_CPP 板


LINE

开发平台(Platform): (Ex: Win10, Linux, ...) Win8.1 编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出) GCC 额外使用到的函数库(Library Used): (Ex: OpenGL, ...) NO 问题(Question): 我最近PYTHON刚转入C++ 虽说知道C++文字处理相较PYTHON困难 但还是想投入,希望能上手 我目前卡住了 我想把INPUT 变成 OUTPUT那种格式 目前已经把INPUT的资料读档丢进vector里 想要从Vector中,去处理文字 我想要像output那样 每一行第一个数字显示这一行共有几组编码 例如 1 xy 2 , xy 2 就是一组编码 又如 5 R 1004 I 5678 E 2000 R 8002 E 7001 R 1004 / I 5678 / E 2000 / R 8002 / E 7001 各自都是一组编码 但目前发现,空白也是vector一个element,单一的数字也是element,xy中x和y各自都是 独立的element 想要请问一下如何在vector中,只要element之间没有space,就合并成一个element 例如 x和y合并 / 1 0 0 4 合并成1004 而且又要排成output的格式 也就是每个文字之间都空一格 喂入的资料(Input): 1 xy 2 2 z xy 5 R 1004 I 5678 E 2000 R 8002 E 7001 0 1 z 6 R 8001 E 1000 E 1000 E 3000 R 1002 A 1010 0 1 z 2 R 5001 E 4000 1 z 2 2 xy z 3 A 8000 E 1001 E 2000 预期的正确结果(Expected Output): 1 xy 2 2 z xy 5 R 1004 I 5678 E 2000 R 8002 E 7001 0 1 z 6 R 8001 E 1000 E 1000 E 3000 R 1002 A 1010 0 1 z 2 R 5001 E 4000 1 z 2 2 xy z 3 A 8000 E 1001 E 2000 错误结果(Wrong Output): 目前有跑过iterator,内圈用while loop,直接前面合并後面 但是code不合法 上网找范例找破头 如果是一开始就不该存进vector的话,也麻烦不吝指教,我想学习判断如何用正确的方式 储存资料 程式码(Code):(请善用置底文网页, 记得排版) #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main() { // Open our file ifstream inFile("inputfile",ifstream::in); // If we can read/write great if (inFile.good()) { // Create a vector of strings std::vector<string> strVector; // Create a string variable to hold each line read from the file. string line; // Read throuthe file and push onto our vector while (!inFile.eof()) { getline(inFile,line); strVector.push_back(line); } // combine character together // create a vector iterator vector<string>::iterator iterate_inFileLine; // loop through the vector from start to end for (iterate_inFileLine = strVector.begin(); iterate_inFileLine < strVector.end(); iterate_inFileLine++){ while (iterate_inFileLine !=""){ strVector[iterate_inFileLine] = strVector[iterate_inFileLine] + strVector[iterate_inFileLine + 1]; } } /* // Create a vector iterator vector<string>::iterator it; // Loop through the vector from start to end and print out the string // the iterator is pointing to. for (it = strVector.begin(); it < strVector.end(); it++) { cout << *it << endl; } */ } inFile.close(); system("pause"); return 0; } 补充说明(Supplement): --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 98.109.25.49
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1496644120.A.9EC.html
1F:→ a29022792: 我觉得可以宣告二维的vector 每个vector存一组编码 06/05 15:34
2F:→ a29022792: 一维存编码 第二维将编码关联起来 06/05 15:36
3F:推 libertyleave: 你可以考虑先简单的用scanf 将你要的资料用正确 06/05 16:15
4F:→ libertyleave: 的格式读入 再存成vector 06/05 16:16
5F:推 libertyleave: 直接把读入的整行丢进 vector 我觉得不好处理 06/05 16:22
6F:→ libertyleave: 把全部都直接串成一个 string 也比直接存vector简单 06/05 16:23
7F:→ libertyleave: 我突然想到可以用inFile>>str呀这样一次只读一个字 06/05 16:32
8F:推 fatrabitree: 基本io题 cin>>处理就好了 06/05 17:41
9F:→ hunandy14: while(fs >> str_p) {cout << str_p << endl;} 06/06 12:01
10F:→ hunandy14: stream 就是那个 >> 预设(可选) 忽略空白跟跳行 06/06 12:05
11F:→ hunandy14: http://ideone.com/I4ir8e 暴力解 想不到好方法QQ 06/09 10:10
12F:→ libertyleave: http://ideone.com/v9PhQ7 我的作法 一样暴力解 06/09 10:36







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP