主题:急!!!求各位好心人帮忙解释一下main主函数中实参的问题,具体问题里面有,谢谢了!!!
问题:各位大侠,我就是想问一下,比如说下面这个程序,在主函数中设有两个实参,我学习过他们的意思,但我就没明白,之后在程序打开文件的过程中,这两个实参都干了什么,起到了什么作用,求好心人在解释时说的详细一些,真的,一定要详细些,谢谢了,谢谢了,我正在为这个问题和流控制的问题纠结,谢谢你们,谢谢了!!!
ps:1.为什末要先判断argc是否小于2呢??
2.不是说argv这个数组中第一个元素存储的是文件名吗,应该是argv【0】啊,为什末这里是打开的argv【1】呢??
// 123.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib> //用于暂停
#include <vector>
#include<fstream>
#include<algorithm>
using namespace std;
//用于将单词排序的比较函数
bool isShorter(const string &s1,const string &s2)
{
return s1.size()<s2.size();
}
//确定给定单词的长度是否不小于
bool GT6(const string &s)
{
return s.size()>=6;
}
//如果ctr不为,返回word的复数版本
string make_plural(size_t ctr,const string &word,const string &ending)
{
return (ctr==1) ? word:word+ending;
}
int main(int argc,char **argv)
{
//检查命令行参数
if(argc<2)
{
cerr<<"No input file!"<<endl;
return EXIT_FAILURE;
}
//打开文件
ifstream inFile;
inFile.open(argv[1]);
if(!inFile)
{
cerr<<"Can not open input file!"<<endl;
return EXIT_FAILURE;
}
vector<string> words;
string word;
//读入要分析的输入序列,并存放在vector容器中
while(inFile>>word)
words.push_back(word);
//对输入排序以便去除重复的单词
sort(words.begin(),words.end());
//使用算法unique对元素重新排序并返回一个迭代器,
//表示无重复的单词范围的结束,
//erase操作使用该迭代器删除输入序列中重复的单词
words.erase(unique(words.begin(),words.end()),words.end());
//将单词按长度排序,等长的单词按字典顺序排列
stable_sort(words.begin(),words.end(),isShorter);
//计算并输出长度不小于的单词的数目
vector<string>::size_type wc=count_if(words.begin(),words.end(),GT6);
cout<<wc<<" "<<make_plural(wc,"word","s")<<" 6 characters or longer"<<endl;
//输出输入序列中不重复的单词
cout<<"unique words:"<<endl;
for(vector<string>::iterator iter=words.begin();iter!=words.end();++iter)
cout<<*iter<<" ";
cout<<endl;
system("pause"); //程序在此处停住,以便检查运行结果!
return 0;
}
ps:1.为什末要先判断argc是否小于2呢??
2.不是说argv这个数组中第一个元素存储的是文件名吗,应该是argv【0】啊,为什末这里是打开的argv【1】呢??
// 123.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib> //用于暂停
#include <vector>
#include<fstream>
#include<algorithm>
using namespace std;
//用于将单词排序的比较函数
bool isShorter(const string &s1,const string &s2)
{
return s1.size()<s2.size();
}
//确定给定单词的长度是否不小于
bool GT6(const string &s)
{
return s.size()>=6;
}
//如果ctr不为,返回word的复数版本
string make_plural(size_t ctr,const string &word,const string &ending)
{
return (ctr==1) ? word:word+ending;
}
int main(int argc,char **argv)
{
//检查命令行参数
if(argc<2)
{
cerr<<"No input file!"<<endl;
return EXIT_FAILURE;
}
//打开文件
ifstream inFile;
inFile.open(argv[1]);
if(!inFile)
{
cerr<<"Can not open input file!"<<endl;
return EXIT_FAILURE;
}
vector<string> words;
string word;
//读入要分析的输入序列,并存放在vector容器中
while(inFile>>word)
words.push_back(word);
//对输入排序以便去除重复的单词
sort(words.begin(),words.end());
//使用算法unique对元素重新排序并返回一个迭代器,
//表示无重复的单词范围的结束,
//erase操作使用该迭代器删除输入序列中重复的单词
words.erase(unique(words.begin(),words.end()),words.end());
//将单词按长度排序,等长的单词按字典顺序排列
stable_sort(words.begin(),words.end(),isShorter);
//计算并输出长度不小于的单词的数目
vector<string>::size_type wc=count_if(words.begin(),words.end(),GT6);
cout<<wc<<" "<<make_plural(wc,"word","s")<<" 6 characters or longer"<<endl;
//输出输入序列中不重复的单词
cout<<"unique words:"<<endl;
for(vector<string>::iterator iter=words.begin();iter!=words.end();++iter)
cout<<*iter<<" ";
cout<<endl;
system("pause"); //程序在此处停住,以便检查运行结果!
return 0;
}