C ++: Why is the code of my (rearranged index) not printing correctly?

I go myself, despite Accelerated problems in C ++, and this is the first that I encounter.

The problem is coding a program that can generate a rearranged index from a set of rows.

The code below has two functions: permutedIndex5_1, which is the "main ()" sorts function and permuteLine5_1, which takes a given line (and a link to a vector of already rearranged lines) and permutes this line by rotation, adding each rotation to the vector.

The problem I am facing is that rebuilt lines do not print correctly on stdout. I have included several debug statements in permuteLine5_1 to test the first and last lines for printing, and the results of these print statements show what should be printed, however, what is printed is completely different.

I feel this may be due to erasing the iterator in this function, but I'm not sure how to fix it. In any case, any help would be appreciated.

EDIT: Content of read text file:

Fast brown fox
slow brown fox
fast 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;
    }*/


}
+3
source share
1 answer

, , , .

...

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());

- , - . , , .

a >

.

+1

Source: https://habr.com/ru/post/1777995/


All Articles