我需要帮助修复试图在字符串中查找子字符串的代码
I need help fixing a code trying to find substring in a string
我正在尝试创建一个新字符串并将月份名称写入其中,然后使用它来检查我是否已经将月份名称写到文件中。这是我试图修复的代码:
void writeToFile(RainStruct rainPerMonth[], int monthsInYear)
{
// Create string variable
string month;
// Create and open file
fstream rainFile("scramble.txt", ios::out);
// Write into file, goes through each structure and writes it
for (int count = 0; count < 11; count++)
{
// Set random number variable
int randNum = rand() % 12;
// Set string variable month to different random month names
month += rainPerMonth[randNum].monthNames;
// Call to checkMonth
if (checkMonth(rainPerMonth, monthsInYear, randNum, month))
{
// Write months in random order to file
rainFile << rainPerMonth[randNum].monthNames << " had " << rainPerMonth[randNum].monthRain << " inches.n";
}
}
// Close, reopen, truncate, and close file again
rainFile.close();
rainFile.open("scramble.txt", ios::trunc);
rainFile.close();
}
bool checkMonth(RainStruct rainPerMonth[], int monthsInYear, int randNum, string month)
{
size_t pos = month.find(rainPerMonth[randNum].monthNames);
if(pos != string::npos)
{
return true;
}
else
{
return false;
}
}
它最终会打印两次某些月份,而不是打印所有月份。我需要更改什么来修复它,以随机顺序将所有月份写入文件(附上每月的降雨量(?这是我的完整代码:https://onlinegdb.com/ry6yO4a4_赋值的规范是,我必须使用一个字符串变量来搜索字符串中的子字符串,然后检查它是否已经存在,然后将月份写入文件。他说我必须使用随机生成器,不能硬编码。这是他给我们的提示:http://www.cplusplus.com/reference/string/string/substr/
您可以使用shuffle()
函数引用中的示例来创建一个月份索引的混洗列表,然后将其写入文件。rand()
不保证唯一索引,因此它可能返回[0, 11]
之间的任何数字,甚至可能重复它们。
#include <algorithm>
#include <array>
#include <iostream>
#include <random>
void print(const auto& a) {
for (const auto e : a) { std::cout << e << ' '; }
std::cout << "n";
}
int main()
{
std::array a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
print(a);
std::random_device rd;
std::mt19937 gen{rd()};
for (int i{}; i != 3; ++i) {
std::ranges::shuffle(a, gen);
print(a);
}
//Then just use a[0], a[1]...as the index to rainPerMonth[] and write it out to file
}
输出:
0 1 2 3 4 5 6 7 8 9 10 11
10 4 6 7 8 11 2 5 9 1 3 0
0 5 7 4 2 8 11 9 6 1 10 3
10 8 5 7 9 4 2 6 11 1 3 0
这在不添加更多依赖项的情况下充分利用了您的需求(尽管std的向量和数组非常好(:
struct RainStruct{
RainStruct(std::string monthNames, int monthRain){
this->monthNames = monthNames;
this->monthRain = monthRain;
}
std::string monthNames;
int monthRain;
};
// Shuffle array
void shuffle_array(int arr[], int n)
{
// To obtain a time-based seed
unsigned seed = 0;
// Shuffling our array
shuffle(arr, arr + n,
std::default_random_engine(seed));
// Printing our array
for (int i = 0; i < n; ++i)
std::cout << arr[i] << " ";
std::cout << std::endl; // you wont need this
}
void writeToFile(RainStruct rainPerMonth[], int monthsInYear)
{
// Create integers array
int nums[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
shuffle_array(nums, 12);
for (int count = 0; count < 12; count++)
{
// Set random number variable
int randNum = nums[count]-1;
std::cout << rainPerMonth[randNum].monthNames << " had " <<
rainPerMonth[randNum].monthRain << " inches.n";
}
}
int main()
{
RainStruct structSample[12] = { RainStruct("Jan", 1) ,
RainStruct("Feb", 2) ,
RainStruct("Mar", 3) ,
RainStruct("Apr", 4) ,
RainStruct("May", 5) ,
RainStruct("Jun", 6) ,
RainStruct("Jul", 7) ,
RainStruct("Agu", 8) ,
RainStruct("Sep", 9) ,
RainStruct("Oct", 10) ,
RainStruct("Nov", 11) ,
RainStruct("Dec", 12) ,
};
writeToFile(structSample, 12);
}
输出:
3 8 11 7 12 1 5 10 6 4 2 9
Mar had 3 inches.
Agu had 8 inches.
Nov had 11 inches.
Jul had 7 inches.
Dec had 12 inches.
Jan had 1 inches.
May had 5 inches.
Oct had 10 inches.
Jun had 6 inches.
Apr had 4 inches.
Feb had 2 inches.
Sep had 9 inches.
您可以使用您的方法来编写文件
问题是,当你要求一堆随机数时,随机数生成器会生成两次相同的数字。随机抽取12个数字,会发现有些数字缺失,有些数字重复。
要解决这个问题,您需要首先以随机顺序构建一个月份编号数组,确保所有月份都在其中,并且没有重复。否则,你会看到你提到的效果。
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 按类型与字符串查找对象
- 使用C RTTI(内置)通过字符串查找功能指针
- 关于获取行和字符串查找函数的问题
- 字符串查找方法找不到第一字母
- C++ 字符串.查找()
- 遇到字符串::查找的问题
- 将一个数组作为子字符串查找到另一个数组中
- 字符串查找第一个非的 C++ 问题
- 如何将通配符与字符串::查找一起使用
- 字符串::查找问题 (C++)
- 性能标准::strstr vs. 标准::字符串::查找
- C++字符串::查找崩溃应用程序
- 使用字符串查找单词的正确方法是什么
- 如何在<string>没有 std::string 中介的情况下制作一个支持通过 C 字符串查找的集合?
- 使用C++根据行中的第一个字符串查找行(仅一个)
- Borland字符串::查找bug
- std::map如何通过字符串查找元素
- 字符串查找函数返回奇数
- 如何使用子字符串查找数字行的正确部分