-6

hi i need to translate my code from java to c++ and researched that vector is the replacement for arraylist. however i have no idea how to go about with it. Below is the initialisation and implementation for questionn array.

ArrayList<Question> questionList = new ArrayList<Question>();
ArrayList<Question> answeredQuestionList = new ArrayList<Question>();
  • 4
    You are saying you couldn't find any resources that explain how to use a `std::vector`? – domsson Mar 15 '17 at 12:14
  • 1
    [A good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or tutorial should have told you everything you need to know. – Some programmer dude Mar 15 '17 at 12:14

2 Answers2

2

You can initialize them in C++ like this:

std::vector<Question> questionList;
std::vector<Question> answeredQuestionList;

These will automatically call the std::vector default constructor, which creates an empty vector.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • is underdefined. is there anything else i should have include instead of ? – Newbie ICT IS Mar 15 '17 at 12:27
  • If Question is undefined, include the file where you define them, if it's in the same file, put Question declaration BEFORE this code. If you need it to be after, use forward declaration. – Sygmei Mar 15 '17 at 12:28
  • Yes, however I'd use std::vector questionList because memory is always allocated on the heap in Java so it makes more sense to just store pointers in the vector. – dsp_user Mar 15 '17 at 12:32
  • Also, regarding the undefined issue, C++ supports forward declarations so it's possible just to add class Question (declaration only) in the file where you define the vector. – dsp_user Mar 15 '17 at 12:34
  • @dsp_user But this isn't Java. In C++ it makes more sense to store the actual objects, not pointers to them (if the container "owns" them). Even if you allocated them on the heap in C++, it would be better to use `unique_ptr` rather than raw pointers. – Emil Laine Mar 15 '17 at 12:45
  • Yes, but remember he's translating some code form Java so I would expect that storing pointers, rather than objects, would be easier to implement (especially since he's a newbie). Also, depending on the number of objects, it might be/should be more efficient. – dsp_user Mar 15 '17 at 12:57
  • need help with defining.. do i just put class Question in the header file which includes it? – Newbie ICT IS Mar 15 '17 at 13:02
  • Well, you don't need to use forward declarations. Just 1) create Question.h (header file) , which contains the definition of the Question class. 2) include Question.h file (#include "Question.h") in the file where you define vector. – dsp_user Mar 15 '17 at 13:08
  • static MCQ[] mcqs = new MCQ[255]; can i know how to convert this sentence of code to c++? – Newbie ICT IS Mar 15 '17 at 14:08
  • Yes you can. If you read a C++ book. Or you know, trivially google the array initialization syntax of C++. – Emil Laine Mar 15 '17 at 14:23
0

new in Java and new in C++ doesn't really have the same signification.

Without pointers version :

std::vector<Question> questionList;
std::vector<Question> answeredQuestionList;

OR

std::vector<Question> questionList({});
std::vector<Question> answeredQuestionList({});

OR

std::vector<Question> questionList = std::vector<Question>();
std::vector<Question> answeredQuestionList = std::vector<Question>();

With pointers version :

std::vector<Question>* questionList = new std::vector<Question>();
std::vector<Question>* answeredQuestionList = new std::vector<Question>();

As new in C++ returns a pointer, the variable type must also be a pointer.

Sygmei
  • 467
  • 2
  • 10
  • 1
    `std::vector questionList();` is a function declaration and `std::vector* questionList = new std::vector();` is useless. – Baum mit Augen Mar 15 '17 at 12:35
  • Corrected the first one ! But the second one is possible (even if it's useless), it was just to show the "new" usage. – Sygmei Mar 15 '17 at 12:39
  • _"`new` in Java and `new` in C++ doesn't really have the same signification. "_ — In fact they _do_ have the exact same meaning: both allocate dynamically and return a pointer to the dynamically allocated object. In Java all object variables just are automatically pointers so you don't need to explicitly specify them as pointers. – Emil Laine Mar 15 '17 at 12:47
  • @tuple_cat, of course, no pointer arithmetics is possible in Java, so they're not pointers in the sense of C++ (but they're similar in some ways) – dsp_user Mar 15 '17 at 13:00