C++ :为什么我的(排列索引)代码打印不正确?

发布于 2024-10-05 07:09:47 字数 2960 浏览 3 评论 0原文

我正在解决 Accelerated C++ 的问题,这是我遇到的第一个问题。

问题是编写一个能够从一组行生成排列索引的程序。

下面的代码有两个函数: permutedIndex5_1 ,它是“main()”函数,以及 permuteLine5_1 ,它采用给定的行(以及对已排列行的向量的引用)并旋转排列给定的行,将每次旋转添加到向量。

我遇到的问题是排列后的行在标准输出上打印不正确。我在 permuteLine5_1 中包含了几个 3 个调试语句来测试要打印的第一个和最后一个字符串,这些打印语句的结果显示了应该打印出的内容,但是打印出来的内容完全是不同的。

我的直觉是,这可能与我删除该函数中的迭代器有关,但我不确定如何纠正它。无论如何,任何帮助将不胜感激。

编辑:读取的文本文件的内容是:

敏捷的棕色狐狸
缓慢的棕色狐狸
敏捷的蓝狗

#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'm walking myself though Accelerated C++'s problems and this is the first one i'm having any sort of trouble with.

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.

The problem i'm having is that the permuted lines are printing out incorrectly on 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.

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;
    }*/


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-10-12 07:09:47

我认为你的迭代器擦除很好,因为你首先制作了字符串的真实副本,并且随后不会使用无效的迭代器。

我会稍微简化排列...

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

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

但是,我认为真正的问题在于排序,我对它的编译感到有点惊讶:

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

您正在尝试对以下向量进行排序 :字符串向量 - 这意味着排序算法需要以某种方式比较两个字符串向量以确定顺序。我认为它不能做到这一点,并且您需要提供自定义排序谓词来执行您想要的精确比较。

感谢 Chris 的更正。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文