Is there a "generics-like" feature in C++? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to create a list class in C++ similar to the lists in Java. Is there a way I can have it be able to list whatever object it wants to? The class resizes arrays to create the list, but what I need to do is find out the kind of object that's needed to store.

Yes, C++ has templates that can be used to create generic containers roughly similar to Java generic containers.
While your immediate reaction might be to assume that std::list is similar to a Java list, that would be a mistake. In Java, a list basically just means a sequence. In C++, a std::list is a linked list (which is rarely useful). Most of the time you want to use an std::vector (which is more like Java's ArrayList).

Yes, there is, and it's called Templates

Related

Set data structure reference [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was asked to create a game using c++, but before I proceed to implement the game I need to research the set data structure from the c++ standard library (STL). I'm looking for a c++ reference that will show how to use this data structure. Also of use would be some information on what it is typically used for, how it works, and what are the pros and cons of using this data structure as opposed to something else.
A set is a data structure that holds unique items. A set in C++ is implemented with a binary search tree, so you need to have an order on the items (comparison function). You can pass in a comparison function to the constructor, or as a template parameter. Here's a resource to get you started on comparisons: http://fusharblog.com/3-ways-to-define-comparison-functions-in-cpp/
The most important things to do with a set are putting in elements, taking out elements, and checking if an element is already in there. These functions are called insert, delete, and find, respectively, for C++.
To list out all the elements, you need to go through them one by one. The begin and end iterator functions let you use a for loop to access each element one by one.
Here are references for the member functions. They contain examples of use.
http://en.cppreference.com/w/cpp/container/set
http://www.cplusplus.com/reference/set/set/
A google search will reveal a lot of links that will give you a description of the data structure.
Here is a link that shows a hands on example; this should get you going...

Bindind to keys C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Hello there I am a realative newbie when it comes to using different "commands" in order to achieve things so I was wondering if any of you know a way to bind a key to do a certain task anywhere in the programme ,so I would be able to display a function for example and after the display finishes the programme carries on normally like nothing happened and then that same key on any other push would still do the display . Thanks in advance
Plain C++ does not have any concept of "key binding". The platform (e.g., the operating system) has this knowledge and it provides some libraries to handle it. So, you must provide more information about the operating system, or use a cross-platform library like Qt.

When is using an iterator appropriate? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm currently taking a class on data structures (in c++) and today we briefly touched iterators. The prof didn't really make much sense of it and while i acquired a basic understanding of what iterators do I was wondering if someone could expand on the topic, possibly giving a Pro's/Con's list of using them vs not preferably in c++ but a general outline would also work.
Thanks in advance to everyone who responds :).
Using an iterator can maintain the protected status of object data. For example, you will probably be iterating through a list. An iterator returned could allow you to step through the list elements (nodes) without being able to directly modify the list. It's implementation dependent, though. You could easily write your own iterator that allows direct modification of the list vectors. It's just another form of convenience abstraction.
An iterator as an interface (pure abstract) could be used to provide a common interface to iterate through different types of lists or arrays, as they are in Java, C#, etc.

Copy control for pointers in string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I have this test and while studying I came up with the following question that I don't even understand (nor the question or the answer)
I know coding, I just don't understand all the nonsense they try to teach us.
So the question is:
Q: The class string has pointers to an array of char. Which of the three copy control solutions for pointers, does the class must define?
A: value_like semantics
I do know that string is an array of chars, but I don't know what are "copy control solutions", I'm not even sure what is copy control (copy constructor maybe??) and what is value_like semantics??
Hope the question makes sense, it was translated from Hebrew.
Thanks for your help :)
"Copy control solutions" probably refers to implementation strategies for copy constructor and copy assignment. I would assume that your textbooks at some point list the three solutions for pointers, given the peculiarly specific wording of the question.
Therefore, look them up and understand what they are about. Knowing how to implement copying for resource-managing objects is one of the most essential language-specific skills for a C++ programmer.

Java ArrayList equivalent in C++ for Tizen [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have experience in Java, but not in C++ and unfortunately I have to write small application in C++ for Tizen. The problem is I have to store data as follows:
data should be stored in one object
ideal object would be java ArrayList (or LinkedList) of ArrayList of Points
How can I achieve that in C++?
Could you propose any sample declaration, definition and get(), add() examples? Is the following a good way to do that:
std::vector<std::vector<Tizen::Graphics::Point> > __strokes;
Use the std::vector class from the standard library
std::vector is a sequence container that encapsulates dynamic size arrays.The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets on regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array.