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...
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
i need to impediment key and value in c like we used in c++ using map.
Please share simplest example to store unique key and value in store in c. key my be any string and value will be string.
Thanks
Creating a hash map requires a bit of work, but is definitely an interesting thing to do.
The general approach to build a hash map is to create an array called buckets that contains keys and values. However, this structure is not able to deal with collisions. A collision between two values may arise when different keys are assigned by the hash function to the same value.
To address this problem a third field, usually a pointer, is added to the bucket. When a collision arises, the new value is added in the data structure pointed at by the third field, usually a linked list or binary tree. For more information about how to resolve collisions, read this link.
An example of the Hash map Structure described above (note the collision at index 153):
To access the hash table, a custom hash function, which returns an integer which identifies the array index location, is applied to the key. Finally, you check if the key used to access the element and the key stored in the array at the index returned by the hash function match. If they do, you have found the right element.
This is just an example; you can find different ways to implement a hash map.
In addition, this question has already been asked here.
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 am relatively new to programming, and I can only really understand C++. I have recently started working on a project that requires the user to input something that will allow them to make a selection. I can't figure out how to make it possible for the user to input a string or a char but get the same result. I know that this would require that I assign the variable that the user inputs (for example 'a') two data types, but how do I do that? I've tried using "string/char a;" but that doesn't work.
Could someone please help me with multi-data-type variables?
Thanks
The string type will work for all user input. Since it "doesn't work" for you, we can't help you further if you don't show us what you tried.
If the User is the one making the Input from the I/O, then you get to decide if you will treat the input as a string or char. After receiving the Input you should know what you want to do with it. And you can also store the input data in array, vectors or list. Primitive data types can do so many things just understand the purpose and function of your program.
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
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.