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

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.

Related

understanding code in c++ [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 6 years ago.
Improve this question
This question arose while studying NS-3 codes. There is a for loop as below
enter code here
for (NetDeviceContainer::Iterator i = periDevice.Begin ();
i != periDevice.End ();
i++)
{
(*i)->GetObject<BleNetDevice> ()->GetLinkLayer ()->SetAdvInterval (Time("1s"));
(*i)->GetObject<BleNetDevice> ()->GetLinkLayer ()->SetRole (BleLinkLayer::ADVERTISER);
(*i)->GetObject<BleNetDevice> ()->GetLinkLayer ()->SetAdvMode (BleLinkLayer::GENERAL_ADV);
}
What is the meaning of above code?
What is Iterator ?
What is (*i)->xxx ?
Which c++ concept is used here.
Thank you in advance.
What is the meaning of above code?
It is a for loop over the objects in the periDevice container, calling the listed functions on each object.
What is Iterator ?
Iterator is a C++ concept (not an C++ exclusive concept) allowing one to iterate over collections. A collection in this context is a data-structure holding multiple objects of the same time (a NetDeviceContainer in your case).
For every iteration of the for loop, the iterator points to one object in the collection.
Some more details here: http://www.cplusplus.com/reference/iterator/
What is (*i)->xxx ?
i is an iterator. Assuming the iterator follows the usual standards, the * is overloaded and returns a reference to the containing object. This object seems to be of some type, for which the -> is defined (most likely a pointer) allowing you to access the "GetObject" function.
Which c++ concept is used here.
Difficult to say, what counts as a c++ concept.
I would say:
for loops
Iterators
Pointers
Operator Overloading
Templates

How do we make our C++ programs fix array index out of range by themselves? [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 7 years ago.
Improve this question
I want to use some C++ code which is not written by me.
However, there are a lot of array index out of range in the code.
Moreover, the lengths of arrays may be not fixed; for example, they may be determined by the size of the input image.
I do not have enough budget to fix them manually, so I ask this question here.
I hope a[i] can be a[0] if i<0 and a[a.Length-1] if i>=a.Length, but I can keep the code a[i].
How do I make it?
You might want to try a wrapper class that is initialized with the original array and then uses an operator[] to behave as you need.
You could write a (templated) class that wrapped an array and overloaded the [] operator to perform bounds checked access to the underlying array. You could then use this class instead of normal C arrays.
How workable this will be will depend heavilly on how the application uses the array. If the array is a gloabl variable or part of a structure/class and is only ever accessed by [] then it will work great but if the array is passed arround by "degrading" to a pointer (and note that array parameters are really pointer parameters) then more work will be needed, changing parameter types and possiblly creating a seperate "checked array reference" class to be used in addition to your "checked array".

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

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.

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