这正确地拉入了一个文本文件,但它没有以正确的顺序输出它,我需要一个线程来让出,但是当我尝试实现它时,它没有工作。每次程序运行时,它都会以随机顺序显示誓言和缺点,即使使用了 yield 函数。
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <thread>
#include <vector>
using namespace std;
void cons(string c){
//Prints consanants
cout <<"CONS: " << c << endl;
this_thread::yield();
}
void vow(string v){
//Prints vowels
// allowing other ready threads to run
this_thread::yield();
cout <<"VOW: " << v << endl;
}
int main() {
//Creates an array of 100
string words[100];
//Creates a starting point for i
int i = 0;
//string called x
string s;
//Takes in a file
ifstream inFile;
//Creates a vector of threads to print
vector <thread> PrintingThreads;
//Opens up the text file "phrase.txt"
inFile.open("phrase.txt");
//If It is not able to open the file
if (!inFile) {
//Display error message
cout << "Unable to open specified file";
//Exit with an error(1)
exit(1);
}
while (inFile >> s) {
words[i]=s;
i++;
}
//cycle
for (int l=0; l<i; l++)
{
char first (words[l][0]);
if ((first == 'a') || (first == 'e') || (first == 'i') || (first == 'o') || (first == 'u')||(first == 'A') || (first == 'E') || (first == 'I') || (first == 'O') || (first == 'U'))
{
/
PrintingThreads.push_back(thread(vow,words[l]));
}
else
{
PrintingThreads.push_back(thread(cons,words[l]));
}
}// loop with a range variable
for (thread& t: PrintingThreads)
t.join();
inFile.close();
}