如何在txt文件(c+;+;ifstream)中获取随机字符串? 例如,我有Txt数据库的单词列表(在屏幕上),我需要在这个文件中随机输出3行,在代码> IFSturiC++中。我在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; } }
升级当前解决方案: 您可以使用动态列表(vector)将文件填充到向量中,然后生成3个随机索引并返回这三个随机字符串的数组。如果文本文件的长度为n,则空间和时间复杂度为O(n) 有更好的解决办法吗? 如果您需要O(1)时间和空间复杂性(不考虑设备上的文件存储),那么我建议将该文件设置为XML文件,因为这将允许您以本地方式在任何索引处访问该文件,这意味着您不需要将整个文件加载到数组中,对于较大的文件,不建议这样做如何在txt文件(c+;+;ifstream)中获取随机字符串? 例如,我有Txt数据库的单词列表(在屏幕上),我需要在这个文件中随机输出3行,在代码> IFSturiC++中。我在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; } },c++,qt,file,fstream,txt,C++,Qt,File,Fstream,Txt,升级当前解决方案: 您可以使用动态列表(vector)将文件填充到向量中,然后生成3个随机索引并返回这三个随机字符串的数组。如果文本文件的长度为n,则空间和时间复杂度为O(n) 有更好的解决办法吗? 如果您需要O(1)时间和空间复杂性(不考虑设备上的文件存储),那么我建议将该文件设置为XML文件,因为这将允许您以本地方式在任何索引处访问该文件,这意味着您不需要将整个文件加载到数组中,对于较大的文件,不建议这样做 int * ReturnRandomWords(){ string curr
int * ReturnRandomWords(){
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;
}
int*ReturnRandomWords(){
串电流;
矢量字文件
ifstream单词(“word_db.txt”);
做{
getline(字,当前);
WordsFromFile.push(当前);
}而(!words.eof());
words.close();
字符串v1=WordsFromFile.at(rand()%WordsFromFile.size());
字符串v2=WordsFromFile.at(rand()%WordsFromFile.size());
字符串v3=WordsFromFile.at(rand()%WordsFromFile.size());
字符串arr[3]={v1,v2,v3};
返回arr;
}
添加到ZanyCactus的答案中
如果您需要每个答案都是唯一的,并且可以使用C++17,那么您可以使用std::sample
:
std::向量随机词;
constexpr大小\u t随机字的数量\u=3;
标准::样品(
WordsFromFile.begin(),
WordsFromFile.end(),
标准::反向插入器(随机字),
随机词的数量,
std::默认随机引擎(std::随机设备())
);
请显示您已经编写的代码。刚刚添加,请检查。您可以添加一个工作示例吗?(尽可能少地包含)刚刚添加,too@Bogdan我相信Riccardo想要的是一个工作的、可移植的示例,我们可以将其放入自己的环境中进行调试/测试。谢谢,但我只是没有在变量ifstream words xD中输入正确的文件名谢谢,但我只是没有在变量ifstream words xD中输入正确的文件名
int * ReturnRandomWords(){
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;
}