C++ 提取字符串中的随机词
我想在给定的行中提取一个随机数,所以我调用了一个随机数,将其用作索引。但它总是给我一个错误C++ 提取字符串中的随机词,c++,string,random,extract,word,C++,String,Random,Extract,Word,我想在给定的行中提取一个随机数,所以我调用了一个随机数,将其用作索引。但它总是给我一个错误 terminated called after throwing an instance of 'std::out_of_range'. 我知道这是因为索引值超出了字符串的大小。但是我找不到我的代码有任何错误 do { int size = line.length(); srand(time(NULL)); index = rand() % size; } while (issp
terminated called after throwing an instance of 'std::out_of_range'.
我知道这是因为索引值超出了字符串的大小。但是我找不到我的代码有任何错误
do {
int size = line.length();
srand(time(NULL));
index = rand() % size;
} while (isspace(line.at(index)));
这是一个随机数发生器。我从文本文件中提取一行
我曾经
do{}while(isspace(line.at(index));
因为我不希望它是空间,为了再次生成一个随机数,如果它是空间
我想要的是这个
0123456789
例如。。。索引:4
F---i---L F:第一,L:最后
if (index == 0) {
while (!isspace(line.at(last + 1))) {
last = last + 1;
}
}
else if (index == (size - 1))) {
while (!isspace(line.at(first - 1))) {
first = first - 1;
}
}
else {
while (!isspace(line.at(first - 1))) {
first = first - 1;
}
while (!isspace(line.at(last + 1))) {
last = last + 1;
}
}
for (int i = first; i <= last; i++) {
targetword += line.at(i);
}
if(索引==0){
而(!isspace(line.at(last+1))){
last=last+1;
}
}
else if(索引==(大小-1))){
而(!isspace(line.at(first-1))){
第一个=第一个-1;
}
}
否则{
而(!isspace(line.at(first-1))){
第一个=第一个-1;
}
而(!isspace(line.at(last+1))){
last=last+1;
}
}
对于(inti=first;i请考虑此循环(或发布的代码段中的以下任何一项):
在调用std::string::at
之前,没有检查增加的last
是否小于字符串的大小,因此最终将抛出std::out\u of_range
类型的异常
另外,std::srand
不应该在循环中重复调用,而应该只调用一次。如果您可以完全避免使用srand
和rand
,移动到现代设施,那就更好了
如果目的是从字符串中“提取随机单词”,则可以将原始字符串拆分为单词,然后选择其中一个
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <string>
#include <cctype>
// Split a string into a vector of words.
auto words_from(std::string const& src)
{
std::vector<std::string> words;
// A "word" can contain only alphanumerical characters. Your milage may vary.
auto is_in_word = [] (char ch) {
return std::isalnum(static_cast<unsigned char>(ch));
};
for (auto it = src.cbegin(); it != src.cend();)
{
auto first = std::find_if(it, src.cend(), is_in_word);
if ( first == src.cend() )
break;
++it;
auto last = std::find_if_not(it, src.cend(), is_in_word);
words.emplace_back(first, last);
it = last;
}
return words;
}
int main()
{
// Initialize the pseudo-random number generator
std::random_device rd;
std::seed_seq ss{rd(), rd(), rd()};
std::mt19937 rnd_gen{ss};
std::string source {"The quick brown fox jumps over the lazy dog."};
// Split the original string into words
auto words = words_from(source);
if (words.size() == 0)
return 0;
// This will generate uniformly distributed indices in the correct range
std::uniform_int_distribution<size_t> dist(0, words.size() - 1);
// Print a random word
std::cout << words[dist(rnd_gen)] << '\n';
}
#包括
#包括
#include.注意您应该只调用srand()
一次-这是随机性的初始“种子”。如果您以相同的方式重复种子,您将重复获得相同的“随机”返回数字。或者更好的是,使用更现代的推荐方法处理随机数:当字符串不包含任何空格时会发生什么?while循环最后递增/先递减,直到超出范围。从C++11开始,STL有一个新的随机数生成器功能(标题),这比C的兰德要好。你可以在网上看到。
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <string>
#include <cctype>
// Split a string into a vector of words.
auto words_from(std::string const& src)
{
std::vector<std::string> words;
// A "word" can contain only alphanumerical characters. Your milage may vary.
auto is_in_word = [] (char ch) {
return std::isalnum(static_cast<unsigned char>(ch));
};
for (auto it = src.cbegin(); it != src.cend();)
{
auto first = std::find_if(it, src.cend(), is_in_word);
if ( first == src.cend() )
break;
++it;
auto last = std::find_if_not(it, src.cend(), is_in_word);
words.emplace_back(first, last);
it = last;
}
return words;
}
int main()
{
// Initialize the pseudo-random number generator
std::random_device rd;
std::seed_seq ss{rd(), rd(), rd()};
std::mt19937 rnd_gen{ss};
std::string source {"The quick brown fox jumps over the lazy dog."};
// Split the original string into words
auto words = words_from(source);
if (words.size() == 0)
return 0;
// This will generate uniformly distributed indices in the correct range
std::uniform_int_distribution<size_t> dist(0, words.size() - 1);
// Print a random word
std::cout << words[dist(rnd_gen)] << '\n';
}