Need help with "vector subscript out of range"

I've had a problem for the past couple days and can't seem to find a solution. I am passing in a wstring and a custom Orthography object. The wstring being passed in is checked against the wstrings from a vector array of custom CharacterMatch objects that my Orthography object has. When it finds a match, I am wanting it to append the output wstring from the CharacterMatch object. I hope I'm not being too confusing and I am unsure how much code you guys would need to see for it to make sense.

Basically it checks each character from the inputted string and checks it against the input values of my CharacterMatch object. When a match is found, it is meant to return the matching output and append it to m_Results.

A bit of tweaking has gotten it to work so long as I only either type in one character or several of the same characters. But the moment I try two different characters, it triggers the error. Stepping through the program shows the GetOutput() adding the appropriate character to the m_Results just fine. It is just when it loops around to the second character and that character is different from the first.

Also, the [0] at the end of the line:

if (wstrInput[i] == orthography.GetCharacterMatches()[j].GetInput()[0])

is temporary. Right now I am just trying one character for one character so I can get away with only checking the first index because right now I am only working with one character wstrings. I'll work up to multi-character versions of this when I get this working. Also, I get red lines under == if I don't have the [0] at the end of that line. I'm not sure why, though.

Here is my function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
vector<wstring> Parser::CustomOrthography(const wstring& wstrInput, const Orthography& orthography)
{
	// Set the size of the vector array to 1 (m_iCount)
	m_iCount = 1;
	m_Results.resize(m_iCount);
	
	// Set the first item in the vector array to an empty string
	m_Results[0] = L"";

	for (int i = 0; i < wstrInput.size(); i++)
	{
		// For each character, need to go through the orthography's character matches
		for (int j = 0; j < orthography.GetCharacterMatches().size(); j++)
		{

			if (wstrInput[i] == orthography.GetCharacterMatches()[j].GetInput()[0])
			{
				m_Results[i] += orthography.GetCharacterMatches()[j].GetOutput();
				continue;
			}
			
		}
	}

	return m_Results;
}


The error occurs at this line:

m_Results[i] += orthography.GetCharacterMatches()[j].GetOutput();

This may sound like a lot of rambling but I hope I was able to convey what is going on. Like I said, I've been stuck on this for a while now and can't seem to get past it. I'm hoping a fresh set of eyes can see what I'm missing :).
Last edited on
closed account (Dy7SLyTq)
your trying to access elements that dont exist
closed account (Dy7SLyTq)
at the beginning do this: your_vector.reserve(900);
your trying to access elements that dont exist

That is true.

at the beginning do this: your_vector.reserve(900);

That would have no effect.

Question: Why is m_Results a member variable? Why is m_iCount a member variable?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <vector>
#include <string>

using std::vector ;
using std::wstring ;


vector<wstring> Parser::CustomOrthography(const wstring& wstrInput, const Orthography& orthography)
{
    std::vector<wstring> results ;

    for (int i = 0; i < wstrInput.size(); i++)
    {
        results.push_back(wstring()) ;

        // For each character, need to go through the orthography's character matches
        for (int j = 0; j < orthography.GetCharacterMatches().size(); j++)
        {
            if (wstrInput[i] == orthography.GetCharacterMatches()[j].GetInput()[0])
                results.back() += orthography.GetCharacterMatches()[j].GetOutput();			
        }
    }

    return results ;

    // It isn't necessary to keep track of the number of elements in results, since we can do
    // results.size() to determine that at any time.
}
Thank you! Changing to "m_Results.back()" worked. And thinking of my response to you on why m_Results is a member variable has shown me why my code wasn't working.

m_Results is a vector of wstrings for my orthography parsing program. I double the number of strings every time I come across a character that could be two different things. I have code set up in other parts of my program to do this and was using the wrong iterator here. Why is it a member function? I can't give you an answer beyond that is the way I made it. I'm still learning to program. As far as the m_iCount, that is used to set the count of m_Results. I didn't want to hardcode a 1 so I made it a variable. However I have another function I made that sets the count and can probably do away with that in this function.

Thanks again!!
Topic archived. No new replies allowed.