我是C ++和C ++ Builder的新手。我以前在python中工作。我正在做一个项目,需要一些帮助。
我正在寻找一种与Python列表相同的类型。我已经尝试了矢量,但对我来说效果不佳。我需要一个变量,可以在其中存储随机数据。我rand()
用来获取数字,但是数字并不总是不同的,它们会重复出现。因此,我尝试了,BoxList
并且它可以在其中存储项目。我已经在Python中做到了,只是让您可以看到我想对大家说的话。
import random
pool= list()
for number in range(1,11):
pool.append(number)
random.shuffle(pool)
print(pool)
这会给我:
[6, 2, 10, 8, 9, 3, 7, 4, 5, 1] # or some other random shuffled numbers
另一个想法是,我可以检查是否正在寻找随机数,BoxList
但是我不知道该怎么做。
编辑:我在c + +生成器中工作,我有输入数字进入我的列表框的问题。
我正在做一个简单的程序,可以帮助我学习。我有100个问题,我想问一个问题(问题的编号),然后如果我的回答正确,则单击一个按钮,如果我的问题错了,则单击另一个按钮。
这是代码:
//---------------------------------------------------------------------------
#include <fmx.h>
#pragma hdrstop
#include <vector>
#include <iostream>
#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm3 *Form3;
int right = 0;
int wrong = 0 ;
int allQuestions = 0;
int currentQuestion = 0;
int toTheEnd = 0;
std::vector<int> asked;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button3Click(TObject *Sender)
{
allQuestions = Edit1->Text.ToInt();
right = 0;
wrong = 0;
Label1->Text = allQuestions;
toTheEnd = allQuestions;
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button1Click(TObject *Sender)
{
right += 1;
toTheEnd -= 1;
Label1->Text = toTheEnd;
Label3->Text = right;
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button2Click(TObject *Sender)
{
wrong += 1;
toTheEnd -= 1;
Label1->Text = toTheEnd;
Label2->Text = wrong;
}
//---------------------------------------------------------------------------
我希望你们能理解我想在这里说的话,请告诉我。
我尚不清楚为什么astd::vector
不适用于您,因为它具有与python列表类型非常相似的属性。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> pool;
for (int i=1; i<11; ++i)
pool.push_back(i);
std::random_shuffle(pool.begin(), pool.end());
for (std::vector<int>::const_iterator i = pool.begin(); i != pool.end(); ++i)
std::cout << *i << " ";
std::cout << "\n";
// Or, you could print this way:
for (int i=0; i<pool.size(); ++i)
std::cout << pool[i] << " ";
std::cout << "\n";
}
此代码输出:
[7:47am][wlynch@watermelon /tmp] ./ex
6 10 7 4 8 9 5 2 3 1
6 10 7 4 8 9 5 2 3 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句