-2

I'm new to C++ and C++ Builder; I was working before in python. I'm working on a project and I need some help.

I'm looking for a type that works the same as list does in Python. I have tried the vector but it doesn't work well for me. I need a variable where I can store random data in. I'm using rand() to get the numbers but the numbers are not always different they will repeat itself. So I tried the BoxList and it works for storing items in it. I Have done it in Python just so you can see what I'm trying to say to you all.

import random
pool= list()
for number in range(1,11):
    pool.append(number)
random.shuffle(pool)
print(pool)

This would give me:

    [6, 2, 10, 8, 9, 3, 7, 4, 5, 1] # or some other random shuffled numbers

The other idea is that I could check if the random number I'm looking for is in the BoxList but I have no idea how to do that.

Edit: Im working in c++ builder and i have problems with getting the number to enter my ListBox.

Im doing a simple program that will help me study. i have like 100 questions and i would like it to ask me a question(the number of the question) and then i click one button if my answer was right and the other if my question was wrong.!

the gui

this is the code:

    //---------------------------------------------------------------------------

    #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;

}
//---------------------------------------------------------------------------

I hope you guyz understand what im trying to say here if its not, plz tell me.

  • 1
    C++ has many nice [algorithms](http://en.cppreference.com/w/cpp/algorithm), for example a function to [randomly shuffle](http://en.cppreference.com/w/cpp/algorithm/random_shuffle) any collection which support random access iterators (like `std::vector`). You might also want to read up on the [PRNG functionality introduced in C++11](http://en.cppreference.com/w/cpp/numeric/random). For initialization, look up the [`std::iota`](http://en.cppreference.com/w/cpp/algorithm/iota) function. – Some programmer dude Jun 27 '14 at 12:48
  • 1
    If you're working in language X, which you do not know very well, you should certainly spend at least 15 minutes learning the basics of language X... Any decent tutorial aimed at programmers coming from other languages should cover basic data structures during the first 15 minutes of studying it. – hyde Jun 27 '14 at 12:54
  • C++ actually has a [list type](http://www.cplusplus.com/reference/list/list/list/) and you can read about the [difference between vector and list here](http://www.differencebetween.net/language/words-language/difference-between-vector-and-list/) – James Jun 27 '14 at 13:11
  • @James I would not recommend this text about vector/list differences. A quote "However, vectors are a bit slow as compared to list objects. Vectors are considered as synchronized objects, efficient in random access, and they properly hold the data with a synced list." I wonder what it may mean. – Wojtek Surowka Jun 27 '14 at 13:19
  • @WojtekSurowka okay, thank you for telling me, do you know of any better articles on the differences? (for myself & OP if they are interested) – James Jun 27 '14 at 13:29
  • 1
    @James Look at http://stackoverflow.com/questions/2209224/vector-vs-list-in-stl – Wojtek Surowka Jun 27 '14 at 13:42
  • @WojtekSurowka very enlightening, great link. – James Jun 27 '14 at 13:57

1 Answers1

4

It's not clear to me why a std::vector won't work for you, because it has very similar properties to python's list type.

#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";
}

This code outputs:

[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 
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173