迭代向量的映射

时间:2016-07-07 14:29:25

标签: c++ dictionary vector iterator

我在尝试迭代矢量地图时遇到了一个奇怪的错误。我在这个项目中使用Stanford CS106B类库,当我尝试编译代码时,我收到一个错误,告诉我" itr没有名为' first' "

我一直在尝试寻找这个问题的解决方案,我找到了许多类似的条目,但答案似乎模仿了我正在做的事情。我确定我错过了一些简单的事情......

#include <iostream>
#include <fstream>
#include <string>
#include "console.h"
#include "simpio.h"  // for getLine
#include "strlib.h"
#include "vector.h"
#include "queue.h"
#include "map.h"
#include <iterator>
using namespace std;

void CountLetters(ifstream &filename) {
    int index=0;
    Vector<int> counts;
    for (int i=0; i<=26; i++) {
        counts.add(0);
    }
    char c;
    while (!filename.eof()) {
        c=filename.get();
        index=c-'a';
        if (index>=0 && index<26) {
            c=stringToChar(toLowerCase(charToString(c)));
            counts[index]++;
        }
    }
    for (int y=0; y<=26; y++) {
        cout << char('a'+y) << ": " << counts[y] << endl;
    }
    filename.close();
}

Map <string, Vector<char> > assembleSets (ifstream &in, int seed) {
    Map <string, Vector<char> > letterSets;
    char c;
    Vector<char> workingText;
    string letterSet;
    while(!in.eof()) {
        if (workingText.size()<seed) {  // Build the intial set of "seed" letters.
            c=in.get();
            workingText.add(c);
        }
        else {
            c=in.get();
            letterSet.clear();
            for (int i=0; i<workingText.size()-1; i++) {
                letterSet+=workingText[i];  // add the letter to the letter set.
                workingText[i]=workingText[i+1]; // move the letter down one in the vector (simulate queue).
            }
            letterSet+=workingText[seed-1];
            workingText[seed-1]=c;  // add the newwest letter to the workingText but do not add it to the letter set.

            // Check to see if the letter set exists already, if not, add it.
            if (!letterSets.containsKey(letterSet)) {
                Vector<char> blank;
                letterSets.add(letterSet,blank);
                letterSets[letterSet].add(c);
            }
            else {
            // Add the next character to the vector of characters for that letter set.
            letterSets[letterSet].add(c);
            }
        }
    }
    return letterSets;
}

int main() {
    ifstream in;
    int mSeed =0;


    while (true) {
        string fileName = getLine("Please enter a file name");

        in.open(fileName);
        if(in.fail()) cout << "Couldn't open file!" << endl;
        else break;
    }
    // CountLetters(in);



    while (true) {
        mSeed=getInteger("Enter a seed value: ");
        if (mSeed>0&&mSeed<=10) {
            break;
        } else {
            cout << "Please choose a value from 1 to 10." << endl;
        }
    }

    Map<string, Vector<char> > letterSets = assembleSets(in, mSeed);
    Map<string, Vector<char> > :: iterator itr;

    for (auto& it: letterSets) {
       string keys = (it.first);
       Vector<char> values = it.second;
    }
    return 0;
}

任何帮助都会很棒!我真的摸不着头脑。

1 个答案:

答案 0 :(得分:0)

它只是意味着Map<string, Vector<char> > :: iterator。 使用std::map代替Mapstd::vector代替Vector正确编译。

检查自定义迭代器的实现。

无论如何,我建议您使用基于范围的语法(如果您使用C ++标准库):

for (auto& it : letterSets)
{
    string key = it.first;
    vector<char> values = it.second;
}