繁体   English   中英

如何在txt文件(c ++ ifstream)中获取随机字符串?

[英]How to get random string in txt file (c++ ifstream)?

例如,我有带有单词列表的 txt db(在屏幕中),我需要在ifstream C++ 中的这个文件中输出随机 3 行。 我在for循环中试过这个,但它非常消耗

#include "gamewindow.h"
#include "ui_gamewindow.h"
#include <iostream>
#include <fstream>
#include <string>
#include <QLabel>
#include <ctime>
using namespace std;
string login;
bool start_is_clicked=false;
GameWindow::GameWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GameWindow)
{
    ui->setupUi(this);
    ifstream logined("last_login.txt");
    getline(logined,login);
    QString qstring_login = QString::fromLocal8Bit(login.c_str());
    ui->login_label->setText(qstring_login);
    logined.close();
}

GameWindow::~GameWindow()
{
    delete ui;
}
int countStringsWords(){
    string current;
    int count = 0;
    ifstream words("word_db.txt");
    do{
        getline(words,current);
        count++;
    }while(!words.eof());
    words.close();
    return count;
}
void getLabelText(){
    srand(time(NULL));
    ifstream words("word_db.txt");
    int start=rand()%countStringsWords();
    words.close();
    cout<<start<<endl;
}
void GameWindow::on_Start_stop_clicked()
{
    if(ui->Start_stop->text()=="START"){
        ui->Start_stop->setText("STOP");
        getLabelText();
    }
    else if(ui->Start_stop->text()=="STOP"){
        ui->Start_stop->setText("START");
    }
    else{
        cout<<"Button error"<<endl;
    }
}

工作区域:

int countStringsWords(){
    string current;
    int count = 0;
    ifstream words("word_db.txt");
    do{
        getline(words,current);
        count++;
    }while(!words.eof());
    words.close();
    return count;
}
void getLabelText(){
    srand(time(NULL));
    ifstream words("word_db.txt");
    int start=rand()%countStringsWords();
    words.close();
}

升级您当前的解决方案:

您可以做的是使用动态列表(向量)用文件填充向量,然后生成 3 个随机索引并返回这三个随机字符串的数组。 如果文本文件的长度为 n,这将是 O(n) 空间和时间复杂度。

有更好的解决方案吗?

如果您想要 O(1) 时间和空间复杂度(不考虑设备上的文件存储),那么我建议您将文件设为 XML 文件,因为这将允许您在任何索引处本地访问文件,这意味着您不需要将整个文件加载到一个数组中,对于较大的文件不建议这样做。

int * ReturnRandomWords(){
    std::string current;
    vector<std::string> WordsFromFile
    ifstream words("word_db.txt");
    do{
        getline(words,current);
        WordsFromFile.push(current);
    }while(!words.eof());
    words.close();
    string v1 = WordsFromFile.at( rand() % WordsFromFile.size());  
    string v2 = WordsFromFile.at( rand() % WordsFromFile.size());   
    string v3 = WordsFromFile.at( rand() % WordsFromFile.size()));
    string arr [3] = {v1, v2, v3};
    return arr;
}

添加到 ZanyCactus 的答案。

如果您需要每个答案都是唯一的并且可以使用 C++17,您可以使用std::sample

std::vector<std::string> random_words;
constexpr size_t number_of_random_words = 3;
std::sample(
    WordsFromFile.begin(),
    WordsFromFile.end(),
    std::back_inserter(random_words),
    number_of_random_words,
    std::default_random_engine(std::random_device()())
);

暂无
暂无

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

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