Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error C2535: member function already defined or declared

I am trying to solve this Facebook challenage

http://learn.hackerearth.com/question/383/staves/

And I am hitting the error

error C2535: 'std::vector<_Ty> &std::map<_Kty,Staves::SubStringVector &>::operator [](const std::basic_string<_Elem,_Traits,_Alloc> &)' 
: member function already defined or declared   
c:\program files (x86)\microsoft visual studio 11.0\vc\include\map  191 1   FB-Staves

I am completely clueless why I get this error, can someone please help me identify issue in my code?

Thanks Sameer

My code is shown below:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

class Staves
{
public:
    struct SubString
    {
    std::string str;
    unsigned int startPos;
    unsigned int endPos;
};

struct PositionalComparer
{
    bool operator()(const SubString* s1, const SubString* s2)
    {
        return s1->startPos < s2->startPos;
    }
} PositionalComparer;

typedef std::vector<const SubString*> SubStringVector;

typedef std::map<const std::string&, SubStringVector&> SubStringMap;

public:
Staves()
{
};

virtual ~Staves()
{
    for(unsigned int i = 0; i < m_vec.size(); ++i)
    {
        delete m_vec[i];
    }
}

public:
void AddSubString(const std::string& input, unsigned int startPos, unsigned int endPos)
{
    SubString* subStr = new SubString();
    subStr->str = input.substr(startPos, endPos - startPos);
    std::sort(subStr->str.begin(), subStr->str.end());
    subStr->startPos = startPos;
    subStr->endPos = endPos;

    m_vec.push_back(subStr);
};

bool PrintStavesIfAny()
{
    SubStringMap subStrMap;

    for(unsigned int i = 0; i < m_vec.size(); ++i)
    {
        const std::string& s1 = m_vec[i]->str;
        if(subStrMap.find(s1) != subStrMap.end())
        {
            SubStringVector& v1 = subStrMap[s1];
            v1.push_back(m_vec[i]);
        }
        else
        {
            SubStringVector v2;
            v2.push_back(m_vec[i]);
            subStrMap[s1] = v2;
        }
    }

    // Now that our map is ready, iterate and find a pair
    // of substrings that do not overlap
    SubStringMap::iterator itr = subStrMap.begin();
    for(; itr != subStrMap.end(); ++itr)
    {
        if(PrintStavesIfAny(itr->second))
        {
            return true;
        }
    }

    return false;
};

private:
bool PrintStavesIfAny(SubStringVector& vec)
{
    std::sort(vec.begin(), vec.end(), PositionalComparer);

    const SubString* s = vec[0];

    for(unsigned int i = 1; i < vec.size(); ++i)
    {
        if(vec[i]->endPos > s->endPos)
        {
            cout << s->startPos << " " << vec[i]->startPos << " " << s->str.size() << endl;
            return true;
        }
    }

    return false;
};

private:
SubStringVector m_vec;
};

void PrintStaves(const string& str)
{
unsigned int currentLen = str.size() / 2;
unsigned int currentPos = 0;

while(currentLen != 0)
{
    // find all substrings of length currentLen
    currentPos = 0;
    Staves* staves = new Staves();

    while(currentPos + currentLen < str.size())
    {
        staves->AddSubString(str, currentPos, currentPos + currentLen);
        ++currentPos;
    }

    // Done adding all substrings of currentLen, now find if a stave exists
    if(staves->PrintStavesIfAny())
    {
        return;
    }

    // make a fresh beginning for a shorter stave
    delete staves;
    --currentLen;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
    string str = "131251141231";
    //getline(cin, str);
    PrintStaves(str);
    return 0;
}
like image 209
indichimp Avatar asked Dec 25 '22 19:12

indichimp


1 Answers

typedef std::map<const std::string&, SubStringVector&> SubStringMap;

STL doesn't allow reseat reference, you can't store reference in STL container. You could store element by value or pointer.

typedef std::map<const std::string, SubStringVector> SubStringMap;
like image 160
billz Avatar answered Dec 28 '22 08:12

billz