尝试从列表中随机选取单个元素并将其显示为C++

Trying to randomly pick a single element from a list and display it in C++

本文关键字:显示 C++ 元素 单个 列表 选取 随机      更新时间:2023-10-16

我是新来的,对编程非常陌生,我花了很多时间搜索互联网和这个网站,我似乎找不到我的这个问题的答案。我需要从字符列表中随机选择一个元素并打印出来,然后重复用户输入的次数。我正在尝试制作一个文本生成器,用于使用单个键而不是单词进行打字练习。(是的,我知道我参加"正确打字"派对已经很晚了,但我对编码的尝试让我看到这是一项比我想象的更有用的技能。
到目前为止,我主要找到了使用迭代器连续打印列表所有元素的答案,这不是我想要的。我也经常遇到有人说使用向量而不是列表,但我开始学习的C++书并没有涉及很多"太高级"的东西,所以我被困在那里。不过,使用列表对我来说似乎是一个好主意,但这可能只是我的初学者谈话。数组对于我想要完成的任务来说太僵化了,因为如果用户可以在该程序的更高版本中输入他们想要的键,而不是像我第一次尝试那样简单地编写它们,那会更理想。 但无论如何,我的程序现在看起来像这样:

// Version 1
// Text Generator for Typing Practice with Individual Keys:
//  For inputting into typing practice programs/sites with "insert your text" fields.
//   Generates a random list of letters from a list of known keys,
//    that only displays up to a user-entered amount of characters,
//     separated into paragraphs.
#include <iostream>
#include <cstdlib>
#include <list>
#include <ctime>
using namespace std;
int main () {
//Declare variables used below
int x = 0;
int list_size = 0;
int p = 0;
// Add new letters to the list below as you learn them!
list <char> Keys_Known = {' ', ' ', ' ', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 'e', 'r', 'u', 'i'};
// Define function(s) used below
char get_Text(); {
for (int i = 0; i <= x; ++i) { // For x amount of times
p = srand(time(nullptr)) % list_size;
// Randomly choose a character from the list by its index,
//  limited to the total number of elements in the list,
cout << Keys_Known[p];
//   and finally output the letter value of the index.
}
}
// Get and set list_size here
list_size = Keys_Known.size();
cout << "Enter total number of characters wanted: " << endl;
cin >> x;
// Set x to the total number of characters wanted in your practice text.
//  Maybe about 100 is good? ((Try with 10 for testing!))
cout << get_Text() << "/n" << get_Text() << "/n";
// Print two blocks of random text separated into paragraphs.
return 0;
}

我不断收到关于 int p 和 int 类型的错误list_size无法使用 %,这就是我通常限制随机数范围的方式。 Eclipse还告诉我,我不能说"Keys_Known[]",因为它不知道[]应该是什么意思。我什至尝试在那里放一个 0,但它仍然表现得像我不能使用索引。我是否缺少"#include"标题? 我得到的确切错误是:">
类型'void'和'int'的无效操作数到二进制'operator%'行29 C/C++问题">
和:">
与'operator[]'不匹配(操作数类型为'std::__cxx11::list'和'int'(第 33 行 C/C++ 问题">

任何帮助将不胜感激,谢谢!

std::list不是
  1. 随机访问容器。因此,您不能使用Keys_Known[p]来访问元素。

    您可以使用数组或std::vector

    数组可以很好地满足您的需求。

  2. 你试图做什么缺乏明确性 - 至少意图没有转化为代码。从一本好书中学习语言的基础知识会很有帮助。

这是您的代码,已更新和注释。希望对您有所帮助。

#include <iostream>
#include <cstdlib>
#include <list>
#include <ctime>
using namespace std;
int main () {
//Declare variables used below
int x = 0;
int list_size = 0;
int p = 0;
// Add new letters to the list below as you learn them!
char Keys_Known[] = {' ', ' ', ' ', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 'e', 'r', 'u', 'i'};
// Seed the random number generator.
srand(time(nullptr));
// Get and set list_size here
list_size = sizeof(Keys_Known)/sizeof(Keys_Known[0]);
cout << "Enter total number of characters wanted: " << endl;
cin >> x;
// Define function(s) used below
// Incorrect.
// This will execute the code in the loop x+1 times.
// for (int i = 0; i <= x; ++i) // For x amount of times
// Correct. Choose one.
// for (int i = 0; i = x; ++i) // For x amount of times
for (int i = 1; i <= x; ++i) // For x amount of times
{
// Get a random number mod list_size..
p = rand() % list_size;
// Randomly choose a character from the list by its index,
//  limited to the total number of elements in the list,
cout << Keys_Known[p];
// and finally output the letter value of the index.
}
cout << endl;
return 0;
}

这里有几件事:

正如 Öö Tiib 所提到的,该函数srand(unsigned int)随机数生成器播种并返回void。 您应该使用srand(unsigned int)为随机数生成器播种一次,然后调用rand()生成一个随机整数。

其次,list<T>不是随机访问容器,因此与其他容器不同,没有定义operator[]。 文档告诉我们,list<T>是作为双向链表实现的,旨在允许恒定时间插入和删除,但缺乏对给定位置元素的直接访问:

与其他列表和

forward_lists相比,列表和的主要缺点 序列容器是它们缺乏对元素的直接访问 他们的职位;例如,要访问列表中的第六个元素, 必须从已知位置(如开头或 end(到该位置,这需要线性时间在 这些。

无论如何,您可能不想在这里使用list<char>。 一些更有效的选择是:

  1. vector<char>,它确实定义了随机访问operator[],并且可以用作代码中的直接替换;
  2. string,它是char的容器,也定义了operator[];
  3. 或者只是一个普通的旧char[list_size],这是你在这里真正需要完成工作的全部,也是我推荐的:你不需要所有花哨的迭代器、搜索方法或动态分配,你从使用 std 容器中获得