understanding code in c++ [closed] - c++

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

Related

Why does C++ containers not have a contains method? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Checking membership is one of the most frequently used operations in any programming languages, but C++ insists that programmers use the verbose container.find(value) != container.end() instead of the readable container.contains(value). What is the rationale behind not adding a syntactic sugar for this operation?
This is not true for all containers. For example; std::set has a .count() member function, that is essentially the same as contains, in that it will return 0 if the container does not contain an element and >0 if it does. There's also std::any_of which works for all containers.
There is a method to check if a container contains an object: std::any_of(). This is also more flexible since it takes a predicate to determine if the object is in the container.

Can we say that a standalone function provides Abstraction? [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 8 years ago.
Improve this question
I am learning about Abstraction, and as I have understood so far, Abstraction is basically providing an interface of how to use an object while hiding the implementation details. But does the concept of Abstraction only applies to OOP, I mean if we think of a standalone function (without being a part of a class), using a function is indeed only using its interface without actually caring of how the function is implemented.
Of course a function provides abstraction.
Why would a bunch of functions and a this pointer provide abstractions while a single function without a this pointer would not?
If, for example you have a function sort() which sorts some data, it abstracts from the concrete sorting algorithm. If you have a function which is the entry point to a huge piece of code consisting of thousands of sub-functions called in the context of that function, it can even be very abstract. Example: GetRouteFromCurrentLocationTo(...). A router, a position sensor, some geographical database... all that abstracted to a single function name.
Why would it be more of an abstraction if you wrote instead: NavigationSystem navSys; navSys.GetRouteFromCurrentLocationTo(...); ?

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

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.