Quantcast
Channel: Forum Pasja Informatyki - Najnowsze pytania
Viewing all articles
Browse latest Browse all 65225

map nie pobiera wartości z vectora

$
0
0

Witam mam pewien problem który wiąże się z tym że nie wiem czemu do map() nie trafiają słowa z vectora (stosuję map pierwszy raz).

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstddef>
#include <map>

using namespace std;

void openFile ( fstream& file );                                //zaladowanie pliku
vector<string> assignment ( fstream& file );                    //przypisanie
vector<string> checkingValues ( vector<string> finishWords );   //sprawdzanie slow
void countingOfWords ( const vector<string>& endChangesWords );        //zliczanie slow


int main()
{
    fstream file;
    openFile( file );
    vector<string> finishWords =  assignment( file );
    vector<string> endChangesWords = checkingValues( finishWords );
    countingOfWords( endChangesWords );
    return 0;
}
//==========================================================================================
void openFile( fstream& file )
{
    file.open( "dane.txt" , ios::in );
    if ( file.good() == false )
    {
        cout << "Plik nie istnieje"<< endl;
    }
}
//==========================================================================================
vector<string> assignment( fstream& file )
{
    string line;
    vector<string> words;
    while( getline( file , line ) )
    {
        words.push_back(line);
    }
    return words;
}
//==========================================================================================
vector<string> checkingValues ( vector<string> finishWords )
{
    unsigned int start = 0 ;
    string wordWithVector;
    while ( start < finishWords.size() )
    {
        wordWithVector = finishWords[start];
        size_t found = wordWithVector.find_first_of("!@#$%^&*()_+|");
        if ( found != string::npos )
        {
            finishWords.erase(finishWords.begin()+start);
        }
        else
            start++;
    }
    return finishWords;
}
//========================================================================================
void countingOfWords ( const vector<string>& endChangesWords )
{
    for ( unsigned int i = 0 ; i < endChangesWords.size() ; i++)
    {
        cout << "to sa słowa: "<< endChangesWords[i] << endl;
    }
    map <string,int> countWords;
    for ( auto const& elem : endChangesWords)
    {
        auto status = countWords.insert(make_pair(elem, 1));
        if ( !status.second )
        {
            ++status.first->second;
        }
    }
    for ( auto const& item : countWords )
    {
        cout << item.first << " : "<< item.second << endl;
    }
}

Wiem już że stosuje złe nazwy po angielsku naprawie to.

I co dziwne wydaje mi się że kod powinien działać bo gdy nie używam <!--StartFragment-->endChangesWords tylko vectora który tam stworze i wpisze sam słowa to zlicza dobrze. A jak kompiluje z <!--StartFragment-->endChangesWords dostaje coś takiego:<!--EndFragment-->

ara : 1
 : 3
 : 1
 : 2
 : 1
 : 1
 : 1c
 : 1

 

<!--EndFragment-->


Viewing all articles
Browse latest Browse all 65225

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>