When is using an iterator appropriate? [closed] - c++

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.

Related

why C++ STL have five different iterators? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
why C++ STL have five iterators? Only random iterator could be sufficient to operate on all the containers. Any specific reason?
Sorry..It is my mistake..I did't mean random iterator...I was supposed to ask about bidirectional iterator...So don't you think that only bidirectional iterator can cover the functionality of input, output, forward iterators? So is there any specific reason to introduce (input, output, forward) iterators concept? Thanks. –
Containers aren't the the only interesting sequence. Also, std::list<...> and the associative containers don't have an efficient method for random access although they are containers. std::forward_list<...> can walk in just one direction. When sequences are sources or drains, they can often just traversed once. Oh, look! I actually gave reasons for all five categories!
Note that the "STL iterators" are not classes but concepts, i.e., requirements for operations and associated types needed to meet the respective iterator concept. The basic idea is that algorithm interfaces are specified in terms of the weakest concepts yielding an efficient implementation. When stronger concepts are provided to the algorithms they may be able to apply some optimizations. This approach yields flexible and efficient algorithms operating on all kinds of different sequences.
To get an idea why check this page
A random access iterator cannot always work. A simple example: If you're streaming data via the network, you cannot start again from the beginning. There are more reasons, but simply read the page.

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...

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.

Is there a "generics-like" feature in C++? [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'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

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.