VB icon

Example of using a vector to store a list of string Update 2

Email
Submitted on: 8/20/2016 10:05:31 PM
By: Ben128  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++, UNIX C++
Views: 2063
 
     This shows how to use a vector to create a list of strings that is broken up when a special char such as a space is found or anything else you assign to split by. Hope it's usfull. UPDATE 2 Added support for spliting strings in closed within quotes

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Example of using a vector to store a list of string Update 2
// Description:This shows how to use a vector to create a list of strings that is broken up when a special char such as a space is found or anything else you assign to split by. Hope it's usfull.
UPDATE 2 Added support for spliting strings in closed within quotes
// By: Ben128
//**************************************

#include <iostream>
#include <vector>
//A simple example of using a vector to store a list of strings
//Works by takeing a input stream then breaking up when a char such as a space of , is found.
using namespace std;
using std::cout;
using std::endl;
void Split(char* src, char del, vector<string>&List){
	//vector<string>List;
	int pos = 0;
	int i = 0;
	char MyWord[128];
	List.clear();
	//Split a string
	while(i < strlen(src)){
		if(src[i] == del){
			//Append ending char
			MyWord[pos] = '\0';
			//Check length
			if(strlen(MyWord) != 0){
				//Push iten into vector
				List.push_back(MyWord);
			}
			pos = 0;
		}
		//If no del is found add chars
		if(src[i] != del){
			//Deal with quotes in the string
			if(src[i] == '"'){
				i+=1;
				//Loop till we find next quote
				while(src[i] != '"'){
					//Build the word
					MyWord[pos] = src[i];
					//INC counters
					pos++;
					i++;
				}
				MyWord[pos] = '\0';
			}else{
				MyWord[pos] = src[i];
			}
			pos++;
		}
		//Keep src counter going.
		i++;
	}
	//Append remaining
	MyWord[pos] = '\0';
	if(strlen(MyWord)){
		List.push_back(MyWord);
	}
	//Clear up
	memset(&MyWord[0],0,sizeof(MyWord));
}
void DisplayVector(vector<string>vec){
	int i = 0;
	
	while(i < vec.size()){
		//Print out items
		cout << (i+1) << " " << vec[i].c_str() << endl;
		i++;
	}
	cout << endl;
}
int main(int argc, char **anvg, char**envp){
	char *Sample = new char[255];
	strcpy(Sample,"Wlecome to \"C++ VB JAVA DELPHI VBA\" Power \"Programming is fun\"");
	//Display contents of vector.
	vector<string>List;
	cout << "Source: " << Sample << endl << endl;
	//Sample one with space
	Split(Sample,' ',List);
	//Show vector contents
	DisplayVector(List);
	strcpy(Sample,"\"Mr Jack Jones\",$2505,18,\"Job Programmer, Designer\"");
	cout << "Source: " << Sample << endl << endl;
	
	Split(Sample,',',List);
	//Show vector contents
	DisplayVector(List);
	delete[]Sample;
	system("pause");
	return 1;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.