Thread: Vector search for matching string.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Vector search for matching string.

    Hi,

    Just working through a problem in Accelerated C++ and it's to determine if a string has been duplicated in a given vector. I have got the program to work and run but I also then get an error box stating "Debug Assertion Failed" - "Expression: vector subscript out of range".

    The code is:

    Code:
    #include <vector>
    #include <iomanip>
    #include <iostream>
    #include <ios>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
    	cout << "Please enter a list of words: ";
    	
    	vector<string> words;
    	string data;
    
    	while (cin >> data)
    		words.push_back(data);
    
    	typedef vector<string>::size_type vec_sz;
    		vec_sz size = words.size();
    
    	// sort vector into alphabetic order
    		sort (words.begin(), words.end());
    
    	//checks for any matching words in the vector
    	
    		for(int x = 0; x <= size; ++x)
    		{
    			if (words[x] == words[x + 1])
    			{
    				cout << "You have a matching pair of words: " << words[x] << " " << words[x+1];
    			}
    
    			else
    			{
    				cout << "No matches";
    			}
    		}
    
    	
    
    	system ("pause");
    
    	return 0;
    
    }
    Any idea why I am getting that error message?

    Thanks,

    Darren.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By using x <= size as the loop condition, you will eventually attempt to access words[size], which does not exist. You should use x < size as the loop condition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    By using x <= size as the loop condition, you will eventually attempt to access words[size], which does not exist. You should use x < size as the loop condition.
    Yes that's worked now thanks, although I have had to use x < size - 1 due to the vector indexing.

    Thanks,

    Darren.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could also structure the loop like:

    Code:
    for (unsigned i = 1; i < size; ++i) {
        if (v[i-1] == v[i])...
    It also looks like you are printing the message "No matches" in a wrong place.
    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).

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by anon View Post
    You could also structure the loop like:

    Code:
    for (unsigned i = 1; i < size; ++i) {
        if (v[i-1] == v[i])...
    It also looks like you are printing the message "No matches" in a wrong place.
    Thanks I changed the location of the "no matches"/

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Another question as well please:

    If I now wanted to determine the string with the shortest number of characters and the one with the most, how would I go about doing that?

    I know how to work out the size of the indiviual strings, however how would you compare them with the others in the vector to determine the shortest and longest?

    Thanks

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by darren78 View Post
    Another question as well please:

    If I now wanted to determine the string with the shortest number of characters and the one with the most, how would I go about doing that?

    I know how to work out the size of the indiviual strings, however how would you compare them with the others in the vector to determine the shortest and longest?

    Thanks
    Generally you compare with < and/or >. You will not, of course, compare every string's size with every other string's size, but go through the vector one by one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM