PENULTIMATE WORD解题思路 codeEval
来源:互联网 发布:mac如何显示包内容 编辑:程序博客网 时间:2024/06/06 22:56
这一题不知道有没有更好的办法,我的思路是先把输入的一行单词放到一个vector<string>中,然后输出倒数第二个。
把一行字符串以空格为分界符拆分成一组字符串,我利用了stl中的find_first_of函数,这个函数可以从指定的位置开始搜索指定字符,返回第一个找到的位置。通过find_first_of函数循环搜索到每一个空格,然后把单词拆分出来。
#include <iostream>#include <fstream>#include <vector>#include <string>using namespace std;void split(const string& s, const string& delim, vector<string> &v) {if(s.size() == 0) return;int last = 0;size_t index = s.find_first_of(delim, last);while(index != string::npos) {v.push_back(s.substr(last, index-last));last = index+1;index = s.find_first_of(delim, last);}if(index - last > 0)v.push_back(s.substr(last, index-last));}int main (int argc, char* argv[]) {ifstream file;string lineBuffer;file.open(argv[1]);while (!file.eof()) {getline(file, lineBuffer);if (lineBuffer.length() == 0)continue; //ignore all empty lineselse {vector<string> v;split(lineBuffer, " ", v);if(v.size() >= 2)cout << v[v.size()-2] << endl;}}return 0;}
0 0
- PENULTIMATE WORD解题思路 codeEval
- LONGEST WORD 解题思路 codeEval
- LeetCode OJ:Word Ladder II 解题思路过程详谈
- 解题思路
- 解题思路
- 解题思路
- 解题思路
- 解题思路
- leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
- leetCode 79.Word Search (词搜索) 解题思路和方法
- 话说解题思路
- 话说解题思路
- 话说解题思路
- POJ1298解题思路
- 二进制问题解题思路
- SSD6 exercise1 解题思路
- zoj 2971 解题思路
- hdu 2147解题思路
- roman to integer
- Power BI的一些视频演示资源
- Qt 实现桌面雪花飘落 - 修改1
- IOS图片压缩和扩展
- C# 2008 windows mobile 6调试环境
- PENULTIMATE WORD解题思路 codeEval
- LONGEST WORD 解题思路 codeEval
- FWNX-C++ version - power
- !!!Obj-c on Mac --- Chapter 10 - 12 Initialization Property Category
- DP16 Floyd Warshall Algorithm @geeksforgeeks
- djang框架学习中遇到的问题总结
- 活期储蓄账目管理系统(C++课程设计)
- 复数计算器
- FWNX- about the abs function -