当前位置: 首页>>代码示例>>C++>>正文


C++ AddressBook::iPush方法代码示例

本文整理汇总了C++中AddressBook::iPush方法的典型用法代码示例。如果您正苦于以下问题:C++ AddressBook::iPush方法的具体用法?C++ AddressBook::iPush怎么用?C++ AddressBook::iPush使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AddressBook的用法示例。


在下文中一共展示了AddressBook::iPush方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: DETAILED

/*
**NOTE**
For all class member functions, check their respective header files
for the DETAILED (Post/Pre condition) description of the function.
*/
int main(int argc,char* argv[])//argv[1] will be the csv inputfile name.
{
 
 //If statement to check if an inputfile command line arguement is passed.
 if(argc<2)
 {
   cout << "\nError usage, please run the program in the following manner:" << endl;
   cout << argv[0] << " CSV_INPUT_FILE_NAME\n" << endl;
   exit(1); //Display usage and exit program if no command line arguement.
 }

//////////////////////////
//TOKEN EXTRACTION BLOCK//
//////////////////////////
 AddressBook bookOne;
 //Create AddressBook object bookOne
 bookOne.pushTitle();
 //This function pushes the title line into the contact vector at position 0.(i.e "First Name", "Last Name" etc...)
 /*Could probably leave this out by reading the first line of the given input file which also contains
 the title line.*/

 vector<string> myString;
 //myString will store text lines from csv inputfile argv[1]
 string fileline, currentline, substring;
 //fileline=line of file from text to be push back into myString
 //currentline=current line that is being looked at by token extraction algorithm
 //substring=token extracted to be pushed into the actual Addressbook Contact


    	//File extration is done here
	ifstream myfile(argv[1]);
	
	//Start extraction line by line if able to open file.
	if(myfile.is_open())
	{
		while(myfile)
		{
			getline(myfile, fileline);
			if(fileline.length()!=0)
			{
				myString.push_back(fileline);
			}
		}
    		myfile.close();
	}

	//Else if the file does not exist, asks user to double check that the files is in the directory.
	else
	{
		cout << "\n*WARNING* CSV File not found in directory. Please check for file:\"" << argv[1] << "\"\n" << endl;
		exit(1);		
	}

    //Token extraction loop/algorithm starts here.
    //Took me a while but I figured it out =)!
    for(int i=1; i<myString.size(); i++) //this for loop will run for the length of myString
    {
        bookOne.iPush();
        //iPush will pushback the vector to make room for the new incomming line.

        currentline=myString.at(i);
        //currentline will get each line from myString from first to end

        //x is left "
        //y is right "
        //token will count token # 1-11 for each field.
        int x=0, y=0, token=0;
        while(token<11)//The algorithm should keep looking for tokens until counter hits 11 tokens
        {
            //x starts looking for the first " from y (0 initially at the start of the string/loop)
            x=currentline.find_first_of("\"", y);
            //y will start looking for the next " starting from x+1
            y=currentline.find_first_of("\"",x+1);

            //This if check is not really needed but to be save, x" will always be less than y"
            if(x<y)
            {
                token++;
            }
            substring=currentline.substr(x+1,(y-x)-1);
            //substring is the token which will always be found at x+1 to y-1
            //to get the length, starting from x+1, it is (y-x)-1...(there are also other ways to calculate)
            bookOne.buildData(i,substring,token);
            //After token is extracted to substring, it is pushed to the address book with it's respective
            //token index counter "token"

            //This was the tricky part...took me a while to figure it out =P
            //y needs to get +1 to prevent x from finding y's " in the next loop. DUHHH!!
            //Also this can be done with a third variable to look for commas but this is more efficient/easier.
            y++;

        }

    }//for loop ends here bringing the extraction process to an end.

//.........这里部分代码省略.........
开发者ID:helloye,项目名称:HunterCSCI,代码行数:101,代码来源:paing_main.cpp


注:本文中的AddressBook::iPush方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。