How to find index of a particular record in a vector?

This is a discussion on How to find index of a particular record in a vector? within the C++ Programming forums, part of the General Programming Boards category; Code: typedef vector<string> vString; vector<vString> vBig; vString vSmall; vString vType; vString::iterator itr; bool bPresent =false; string type="phone"; string val="857867"; for(itr=vType.begin();itr!=vType.end();itr++) ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Question How to find index of a particular record in a vector?

    Code:
    	typedef vector<string> vString;
    	
    	vector<vString> vBig;
    	vString vSmall;
    	vString vType;
    	vString::iterator itr;
    	bool bPresent =false;
    string type="phone";
    	string val="857867";
    
    for(itr=vType.begin();itr!=vType.end();itr++)
    	{
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    In above code , how I will get index inside vector , where my search string is found?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why do you need the index, is the iterator not good enough?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Yes.
    But In my program I have two vectors.
    One for storing keys and other for storing values.
    So I need index as well.
    Code:
    #include<vector>
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	
    	typedef vector<string> vString;
    	
    	vector<vString> vBig;
    	vString vSmall;
    	vString vType;
    	vString::iterator itr;
    	bool bPresent =false;
    
    	string type="phone";
    	string val="857867";
    
    	bPresent =false;
    	for(itr=vType.begin();itr!=vType.end();itr++)
    	{
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    	if(bPresent)
    	{
    
    	}
    	else
    	{
    		vType.push_back(type);
    		vBig.push_back(vSmall);
    		vBig[0].push_back(val);
    	}
    
    	type="telephone";
    	val="222";
    
    
    	bPresent =false;
    	for(itr=vType.begin();itr!=vType.end();itr++)
    	{
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    	if(bPresent)
    	{
    
    	}
    	else
    	{
    		vType.push_back(type);
    		vBig.push_back(vSmall);
    		vBig[0].push_back(val);
    	}
    
    	type="telephone";
    	val="222";
    
    }
    Can you pleas help me in finding index?

    Thanks in advance
    Ketu

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't understand from your code where you need the index, please explain...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Thanks a lot matsp.
    It is solved now.
    Final program is as below:
    Code:
    #include<vector>
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	
    	typedef vector<string> vString;
    	
    	vector<vString> vBig;
    	vString vSmall;
    	vString vType;
    	vString::iterator itr;
    	vString::iterator itrValue;
    	vector<vString>::iterator itrBig;
    	int iSize =0;
    
    
    	bool bPresent =false;
    	int iIndex=0;
    	//int iIndexStore=0;
    
    	string type="telephone";
    	string val="857867";
    
    	bPresent =false;
    
    	for(itr=vType.begin(),	iIndex=0;itr!=vType.end();itr++,iIndex++)
    	{
    
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    	if(bPresent)
    	{
    			vBig[iIndex].push_back(val);
    	}
    	else
    	{
    		vType.push_back(type);
    		vBig.push_back(vSmall);
    		vBig[iIndex].push_back(val);
    		/*
    		iSize=vBig.size();
    		cout<<"\n Size of VBig :" <<iSize<<endl;
    		vBig[iSize-1].push_back(val);
    		*/
    
    
    	}
    
    	//-------------------------------------------------------------
    	type="telephone";
    	val="333";
    	
    	bPresent =false;
    	//iIndex=0;
    	for(itr=vType.begin(),iIndex=0;itr!=vType.end();itr++,iIndex++)
    	{
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    	if(bPresent)
    	{
    
    		vBig[iIndex].push_back(val);
    
    	}
    	else
    	{
    		vType.push_back(type);
    		vBig.push_back(vSmall);
    		vBig[iIndex].push_back(val);
    		/*
    			iSize=vBig.size();
    		cout<<"\n Size of VBig :" <<iSize<<endl;
    		vBig[iSize-1].push_back(val);
    	*/
    	}
    
    
    	type="mail";
    	val="a@b.com";
    	bPresent =false;
    	
    	for(itr=vType.begin(),iIndex=0;itr!=vType.end();itr++,iIndex++)
    	{
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    	if(bPresent)
    	{
    
    		vBig[iIndex].push_back(val);
    
    	}
    	else
    	{
    		vType.push_back(type);
    		vBig.push_back(vSmall);
    		vBig[iIndex].push_back(val);
    
    		/*
    			iSize=vBig.size();
    	cout<<"\n Size of VBig :" <<iSize<<endl;
    		vBig[iSize-1].push_back(val);
    		*/
    	
    	}
    
    //-------------------------------------------------------------
    	type="mail";
    	val="p@qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq.com";
    	
    	bPresent =false;
    	//iIndex=0;
    	for(itr=vType.begin(),iIndex=0;itr!=vType.end();itr++,iIndex++)
    	{
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    	if(bPresent)
    	{
    
    		vBig[iIndex].push_back(val);
    
    	}
    	else
    	{
    		vType.push_back(type);
    		vBig.push_back(vSmall);
    	vBig[iIndex].push_back(val);
    	/*
    			iSize=vBig.size();
    	cout<<"\n Size of VBig :" <<iSize<<endl;
    		vBig[iSize-1].push_back(val);
    	*/
    	}
    
    	type="fax";
    	val="777";
    	bPresent =false;
    	//iIndex=0;
    	for(itr=vType.begin(),iIndex=0;itr!=vType.end();itr++,iIndex++)
    	{
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    	if(bPresent)
    	{
    
    		vBig[iIndex].push_back(val);
    
    	}
    	else
    	{
    		vType.push_back(type);
    		vBig.push_back(vSmall);
    		vBig[iIndex].push_back(val);
    		/*
    					iSize=vBig.size();
    		cout<<"\n Size of VBig :" <<iSize<<endl;
    		vBig[iSize-1].push_back(val);
    	*/
    	}
    
    	//-------------------------------------------------------------
    	type="mail";
    	val="x@rtyr.com";
    	
    	bPresent =false;
    	//iIndex=0;
    	for(itr=vType.begin(),iIndex=0;itr!=vType.end();itr++,iIndex++)
    	{
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    	if(bPresent)
    	{
    
    		vBig[iIndex].push_back(val);
    
    	}
    	else
    	{
    		vType.push_back(type);
    		vBig.push_back(vSmall);
    		vBig[iIndex].push_back(val);
    		/*
    					iSize=vBig.size();
    		cout<<"\n Size of VBig :" <<iSize<<endl;
    		vBig[iSize-1].push_back(val);
    	*/
    	}
    
    	type="mail";
    	val="w@yuygt";
    	bPresent =false;
    	//iIndex=0;
    	for(itr=vType.begin(),iIndex=0;itr!=vType.end();itr++,iIndex++)
    	{
    		string data = *itr;
    		if(type.compare(data)==0)
    		{
    			bPresent=true;
    			break;
    		}
    	}
    	if(bPresent)
    	{
    
    		vBig[iIndex].push_back(val);
    
    	}
    	else
    	{
    		vType.push_back(type);
    		vBig.push_back(vSmall);
    		vBig[iIndex].push_back(val);
    		/*
    					iSize=vBig.size();
    		cout<<"\n Size of VBig :" <<iSize<<endl;
    		vBig[iSize-1].push_back(val);
    		*/
    	
    	}
    //Parse Values
    
    	for (itr=vType.begin(),iIndex=0;itr!=vType.end();itr++,iIndex++)
    	{
    		string strType = *itr;
    		const char * pchType = strType.data();
    		cout<<"\nValues for Type : "<<pchType <<" are \n ";
    
    
    		string strValue;
    		for(itrValue= vBig[iIndex].begin();itrValue!=vBig[iIndex].end();itrValue++)
    		{
    			strValue=*itrValue;
    			const char * pchVal= strValue.data();
    			cout<<endl<<pchVal;
    		}
    	}
    	cout<<endl<<"============================================="<<endl;
    
    	
    return 0;
    
    }

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks like you want to associate a number of values with a string keyword. For that a std::map<std::string, std::vector<std::string> > might be appropriate.

    Code:
        map<string, vector<string> > data_map;
        string type="telephone";
        string val="857867";
    
        //adds val to the vector corresponding to type if the key exists
        //else adds the key and adds val
        data_map[type].push_back(val);


    Other than that you might read about functions to get rid of the redundancies in your code.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by ketu1 View Post
    In above code , how I will get index inside vector , where my search string is found?
    If you really need it, then the distance STL function perhaps?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    what you have here is linear search, which doesn't scale well. If the vector can get large by any chance, try Binary Search.

    This is assuming you only need to search sparingly (compared to index dereferencing). Otherwise, as anon suggested, use a map.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector Addition Using OOP?
    By everydaybh in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2009, 05:09 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 09:14 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21