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.