关于命令行å‚数输入的问题
问题 1:关于void insert(const T &x,BinaryNode * &t)
è€å¸ˆæ‚¨ä¸Šè¯¾è®²äº†*&tï¼Œè¯´æ˜¯åŠ å¼•ç”¨æ˜¯ä¸ºäº†æ”¹å˜t所指å‘的地å€ï¼Œæˆ‘觉得讲得ä¸æ˜¯å¤ªæ¸…楚。我的ç†è§£æ˜¯ï¼Œæ¯”如,
struct BinaryNode
{
string element;
BinaryNode* left;
BinaryNode* right;
TreeNode(BinaryNode* m=NULL,BinaryNode* n=NULL):left(m),right(n){}
};
insert(x,p);
则BinaryNode*类型的å˜é‡åªæ˜¯ç»“构体ä¸çš„一个æˆå‘˜,void insert(const T &x,BinaryNode * &t)改为void insert(const T &x,BinaryNode * t),则æ¤æ—¶çš„p仅仅表示一个æˆå‘˜çš„地å€ï¼Œè€Œä¸æ˜¯è¿™ä¸ªç»“构体的地å€ã€‚æ•…æ¤æ—¶çš„*&t我认为是代表这个结构体的地å€ã€‚
问题 2:
我在网上查了相关命令行å‚数输入的问题,å¯æ˜¯è¿˜æ˜¯ä¸æ‡‚如何在codeblocks输入å‚数。。。请è€å¸ˆç»™å‡ºå…·ä½“çš„æ¥éª¤å•Š
如我下é¢çš„例å(c++primerç›¸å…³ç« èŠ‚), ä¿å˜è·¯å¾„D:\My Documents\c++\argc_argv
#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
#include<cstdlib>
using namespace std;
const char *const program_name = "comline";
const char *const program_version = "version 0.01 (08/07/97)";
inline void usage( int exit_value = 0 )
{
// è¾“å‡ºä¸€ä¸ªæ ¼å¼åŒ–的用法信æ¯
// 并用 exit_value 退出...
cerr<< "usage:\n"
<< program_name << " "
<< "[-d] [-h] [-v] \n\t"
<< "[-o output_file] [-l limit] \n\t"
<< "file_name\n\t[file_name [file_name [ ... ]]]\n\n"
<< "where [] indicates optional option: \n\n\t"
<< "-h: help.\n\t\t"
<< "generates this message and exits\n\n\t"
<< "-v: version.\n\t\t"
<< "prints version information and exits\n\n\t"
<< "-d: debug.\n\t\tturns debugging on\n\n\t"
<< "-l limit\n\t\t"
<< "limit must be a non-negative integer\n\n\t"
<< "-o ofile\n\t\t"
<< "file within which to write out results\n\t\t"
<< "by default, results written to standard output \n\n"
<< "file_name\n\t\t"
<< "the name of the actual file to process\n\t\t"
<< "at least one file_name is required --\n\t\t"
<< "any number may be specified\n\n"
<< "examples:\n\t\t"
<< "$command chapter7.doc\n\t\t"
<< "$command -d -l 1024 -o test_7_8 "
<< "chapter7.doc chapter8.doc\n\n";
exit( exit_value );
}
int main( int argc, char* argv[] )
{
bool debug_on = false;
bool ofile_on = false;
bool limit_on = false;
int limit = -1;
string ofile;
vector<string> file_names;
cout << "illustration of handling command line arguments:\n"
<< "argc: " << argc << endl;
for ( int ix = 1; ix < argc; ++ix )
{
cout << "argv[ " << ix << " ]: "
<< argv[ ix ] << endl;
char *pchar = argv[ ix ];
switch ( pchar[ 0 ] )
{
case '-':
{
cout << "case \'-\' found\n";
switch( pchar[ 1 ] )
{
case 'd':
cout << "-d found: "
<< "debugging turned on\n";
debug_on = true;
break;
case 'v':
cout << "-v found: "
<< "version info displayed\n";
cout << program_name
<< " :: "
<< program_version
<< endl;
return 0;
case 'h':
cout << "-h found: "
<< "help information\n";
// 这里没必è¦ç”¨ break 了, usage() å¯ä»¥é€€å‡º
usage();
case 'o':
cout << "-o found: output file\n";
ofile_on = true;
break;
case 'l':
cout << "-l found: "
<< "resource limit\n";
limit_on = true;
break;
default:
cerr << program_name
<< " : error : "
<< "unrecognized option: - "
<< pchar << "\n\n";
// 这里没必è¦ç”¨ break 了, usage() å¯ä»¥é€€å‡º
usage( -1 );
}
break;
}
default: // 或文件å
cout << "default nonhyphen argument: "
<< pchar << endl;
if ( ofile_on ) {
ofile_on = false;
ofile = pchar;
}
else
if ( limit_on ) {
limit_on = false;
limit = atoi( pchar );
if ( limit < 0 ) {
cerr << program_name
<< " : error : "
<< "negative value for limit.\n\n";
usage( -2 );
}
}
else file_names.push_back( string( pchar ));
break;
}
}
if ( file_names.empty() ) {
cerr << program_name
<< " : error : "
<< "no file specified for processing.\n\n";
usage( -3 );
}
if ( limit != -1 )
cout << "User-specifed limit: "
<< limit << endl;
if ( ! ofile.empty() )
cout << "User-specified output file: "
<< ofile << endl;
cout << (file_names.size() == 1 ? "File " : "Files ")
<< "to be processed are the following:\n";
for ( int inx = 0; inx < file_names.size(); ++inx )
cout << "\t" << file_names[ inx ] << endl;
}