これはテキスト ファイルを適切に取り込みますが、正しい順序で出力しません。生成するスレッドが 1 つ必要ですが、実装しようとしても機能しません。プログラムが実行されるたびに、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();
}