vendredi 28 octobre 2022

Is there a better algorithm for this code? for storing of particular text from .txt file to std::vector<:string>?

I have this name_and_id.txt file which contains Name of Users and its IDs.

The file contains:

0. Heal: A24-1234
1. Hael: A25-4567
2. Hela: A26-8910
3. Hale: A27-1112

What I wanted to do is to get rid of the Name of Users and to just left with IDs only. In my first output it works fine -

First output: (works fine)

A241234
A254567
A268910
A271112

But if I add more Name of Users and IDs (add new line first before the new name of users and ids)

Adding new Name of Users and IDs:

0. Heal: A24-1234
1. Hael: A25-4567
2. Hela: A26-8910
3. Hale: A27-1112

4. Test1: A28-1314
5. Test2: A29-1516
5. Test3: A30-1718

In my second try, it gives me an output like this one:

A241234
A254567
A268910
A271112
Test1
Test2
Test3

Here's my code:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <utility>
#include <vector>

auto main() -> int {
  
    std::fstream user_file{};
    
    const char space_char{' '}; //NOTE - space delimiter 
    std::string entries_by_line{};
    std::vector<std::string> data_entries{};
    std::stringstream entries_in_file{};
    std::string entries_data_imported_file{};
    std::vector<std::string> tokenized_data_imported_file{};
  
    user_file.open("name_and_id.txt");
    if(user_file.is_open()) {
        while(std::getline(user_file, entries_by_line)) { 
            data_entries.emplace_back(entries_by_line); //REVIEW - store data (without new lines) from `.txt` file into vector named `data_entries`
        }
  
        std::copy(
            data_entries.begin(),
            data_entries.end(),
            std::ostream_iterator<std::string>(entries_in_file, " ") //REVIEW - copy contents (with spaces) of vector `data_entries` to a std::stringstream named `entries_in_file`
        );
    
  
        std::stringstream entries{std::move(entries_in_file.str())}; //REVIEW - store content of entries_in_file.str() to std::stringstream named `entries`
    
        while(std::getline(entries, entries_data_imported_file, space_char)) {
            entries_data_imported_file.erase(
                std::remove_if(
                    entries_data_imported_file.begin(),
                    entries_data_imported_file.end(),
                    ispunct //REVIEW - remove punctuations
                ),
                entries_data_imported_file.end()
            );
            tokenized_data_imported_file.emplace_back(entries_data_imported_file); //REVIEW - store data (without punctuations) to vector `tokenized_data_imported_file`
        }
    
        if((tokenized_data_imported_file.size() % 2) > 0) { 
            tokenized_data_imported_file.pop_back(); //REVIEW - remove every odd values in vector `tokenized_data_imported_file` to only left the rhs text
        }
      
        for(size_t i{}; i != (tokenized_data_imported_file.size() / 2); ++i) {
            tokenized_data_imported_file.erase(tokenized_data_imported_file.begin() + i); 
            std::cout << tokenized_data_imported_file[i] << std::endl;
        }
    }
    user_file.close();
  
}

Can you also guys suggest a better algorithm for this type of problem?

Aucun commentaire:

Enregistrer un commentaire