在刷leetcode1078的时候,突然想尝试通过代码获取输入字符到stringstream,并通过‘ ’拆分输入.然后搞定了如下代码.
#include<iostream>
#include <sstream>
#include<string>
#include<vector>
using namespace std;
int main(int argc,char*argv[]){
string text;
cout<<*argv<<endl;
int pos = 1;
if(argc > 1){//only when parameter bigger than 1
while(pos < argc){
char *tmp = argv[pos];
while(*tmp){
text.push_back(*tmp);
tmp++;
}
text.push_back(' ');
pos++;
}
cout<<text<<endl;
/*other funcition,print the split words*/
vector<string> words;
stringstream ss(text);
string item;
while(getline(ss,item,' ')){
words.push_back(item);
cout<<item<<endl;
}
}
return 0;
}
引用
main函数参数解析以及命令行参数_xiaobai-CSDN博客【Linux】main函数原型如下:main( int argc, char *argv[ ], char *envp[ ] ){program-statements}可知,main函数是有参数的,第一个参数:argc是一个整形变量,表示命令行参数的个数(含第一个参数,即目录)例如在Linux下 ./a.out -a -b -c -4... 这一段命令由多少个空格隔开多少个区域,arg...
https://blog.csdn.net/weixin_41318405/article/details/80038771C++ find_if()和find_if_not()函数用法详解继《 C++ find()函数 》一节后,本节再讲解一个和 find() 功能类似的函数,即 find_if() 函数。 和 find() 函数相同,find_if() 函数也用于在指定区域内执行查找操作。不同的是,前者需要明确指
http://c.biancheng.net/view/7493.html
本文介绍了如何在C++中使用main函数解析命令行参数,并利用stringstream处理输入字符串,实现通过空格拆分单词的功能。展示了如何读取argv数组,构建字符串,并使用getline和stringstream进行有效操作。
425

被折叠的 条评论
为什么被折叠?



