简体   繁体   English

C ++:为什么我的(置换索引)代码不能正确打印出来?

[英]C++ : Why isn't my (permuted index) code printing out correctly?

I'm walking myself though Accelerated C++'s problems and this is the first one i'm having any sort of trouble with. 我正在走自己的加速C ++的问题,这是第一个我遇到任何麻烦。

The problem is to code a program that is able to generate a permuted index from a set of lines. 问题是编写一个能够从一组行生成置换索引的程序。

The code below has two functions: permutedIndex5_1 which is the "main()" function of sorts, and permuteLine5_1 which takes a given line (and a reference to an vector of already permuted lines) and permutes the given line rotationally, adding each rotation to the vector. 下面的代码有两个函数:permutedIndex5_1,它是排序的“main()”函数,permuteLine5_1采用给定的行(以及对已经置换的行的向量的引用)并且旋转地置换给定的行,将每个旋转添加到矢量。

The problem i'm having is that the permuted lines are printing out incorrectly on stdout. 我遇到的问题是转换线在stdout上打印不正确。 I've included several 3 debug statements in permuteLine5_1 to test the first and last strings to be printed, and the results of those print statements show what should be printed out, however what is being printed out is completely different. 我在permuteLine5_1中包含了几个3个调试语句来测试要打印的第一个和最后一个字符串,这些打印语句的结果显示了应该打印出来的内容,但是打印出来的内容完全不同。

My gut feeling is that it may have to do with me erasing the iterator in that function, but i'm not sure how to rectify it. 我的直觉是它可能与我擦除该函数中的迭代器有关,但我不确定如何纠正它。 In any case, any help would be appreciated. 无论如何,任何帮助将不胜感激。

EDIT: The contents of the text file read are : 编辑:读取的文本文件的内容是:

The quick brown fox 快速的棕色狐狸
The slow brown fox 慢棕狐
The quick blue dog 快速的蓝狗

#include <vector>
#include <string>
#include <cctype>
#include <iostream>
#include <fstream>
#include <sstream>


using std::fstream;
using std::ios;
using std::istringstream;
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;

void permuteLine5_1(vector< vector<string> >& lines, vector<string> curLine)
{

    for(int i = 0; i < curLine.size(); i++)
    {
        vector<string>::iterator curBeginStrItr = curLine.begin();
        string curBeginStr = *curBeginStrItr;

        curLine.erase(curBeginStrItr);
        curLine.push_back(curBeginStr);

        cout << "The first string in the current line is : "  + *(curLine.begin()) << endl;
        cout << "The first string in the current line is VIA INDEXING IS : "  + curLine[0] << endl;
        cout << "The last string in the current line is : "  + *(curLine.rbegin()) << endl;

        for(int j = 0; j < curLine.size(); j++)
        {
            cout << curLine[j];
        }
        cout << endl;

        lines.push_back(curLine);
    }


}

void permutedIndex5_1()
{
    vector< vector<string> > lines;

    fstream fileLines;
    fileLines.open("C:\\Users\\Kevin\\Desktop\\lines.txt", ios::in);

    string curLine, curWord;
    vector<string> curLineVec;

    while(getline(fileLines, curLine))
    {
        cout << curLine << endl;

        curLineVec.push_back("|");

        istringstream strS(curLine);

        while(getline(strS, curWord, ' '))
        {
            curLineVec.push_back(curWord);
            cout << curWord << endl;  
        }

        lines.push_back(curLineVec);
        curLineVec.clear();
    }

    vector< vector<string> > permuted;

    for(int i = 0; i < lines.size(); i++)
    {
        permuteLine5_1(permuted, lines[i]);
    }

    sort(permuted.begin(), permuted.end());

    /*Code below prints out permutations. Commented out because
      permuting function does not work properly

    for(int i = 0; i < permuted.size(); i++)
    {
        vector<string> curVec = permuted[i];
        for(int j = 0; j < curVec.size(); j++)
        {
            cout << curVec[j] << ' ';
        }

        cout << endl;
    }*/


}

I think your iterator erasing is fine, because you make a real copy of the string first, and your invalidated iterator is not subsequently used. 我认为你的迭代器擦除很好,因为你首先创建了一个字符串的真实副本,并且随后不使用你的无效迭代器。

I'd simplify the permutation a bit to... 我稍微简化了排列......

for(int i = 0; i < curLine.size(); i++)
{
    string curBeginStr = curLine[0];

    curLine.erase(curLine.begin());
    curLine.push_back(curBeginStr);

But, I think the real problem is in the sort, and I'm a bit surprised it compiles: 但是,我认为真正的问题是那种,我有点惊讶它编译:

 
 
 
  
  sort(permuted.begin(), permuted.end());
 
  

You're trying to sort a vector of vectors of string - which means that the sort algorithm needs to somehow compare two vectors of strings to determine order. 您正在尝试对字符串向量的向量进行排序 - 这意味着排序算法需要以某种方式比较两个字符串向量以确定顺序。 I think it can't do that, and that you'll need to provide a custom sorting predicate to perform the exact comparison you want. 我认为它无法做到这一点,并且您需要提供自定义排序谓词来执行您想要的精确比较。

Thanks to Chris for the correction. 感谢克里斯的纠正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM