We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Category: STL

Be careful with hidden cost of std::vector for user defined objects

output_1

While creating and using std::vector with user defined classes we needs some special care otherwise it can hamper the performance of our application. [showads ad=inside_post] Let’s learn by example, Suppose we have an Item Class, Advertisements class Item { public: static int m_ConstructorCalledCount; static int m_DestCalledCount; static int m_CopyConstructorCalledCount; Item() { m_ConstructorCalledCount++; } ~Item() { …

Be careful with hidden cost of std::vector for user defined objectsRead More

C++ : Map Tutorial -Part 2: Map and External Sorting Criteria / Comparator

In this article we will discuss how to use external sorting criteria for keys in std::map and points that we need to take care with external sorting criteria. Default sorting criteria for keys in std::map is operator “<” i.e. std::less<T>. So, while creating std::map if we don’t specify the external sorting criteria then default criteria …

C++ : Map Tutorial -Part 2: Map and External Sorting Criteria / ComparatorRead More

C++ : Map Tutorial Part 3: Using User defined class objects as keys in std::map

In this article we will discuss how to use User defined classes as key in std::map. By default std::map uses “operator <” as sorting criteria for keys. For default data types like int and std::string etc, operator < is available by default but for User defined classes operator < is not available by default. Advertisements …

C++ : Map Tutorial Part 3: Using User defined class objects as keys in std::mapRead More

Using STL to verify brackets or parentheses combination in an expression

In this we will see how to check if a given string take valid combination of open and close brackets i.e. (4+{8-[22+8]*}] contains valid order of open and close brackets. ({5+8]) contains invalid combination of open and close brackets. Advertisements [showads ad=inside_post] Bracket combinations to be used, Frequently Asked: Using STL to verify brackets or parentheses combination in an …

Using STL to verify brackets or parentheses combination in an expressionRead More

Remove all occurences of an element from vector in O(n) complexity

Suppose we have a vector of integers and we want to delete all occurences of a number from it i.e. Let’s say vector contain following numbers 1,2,5,4,5,1,5,7,8,9. Now we want to delete all the occurences of 5 from it, so that vector contents should become – 1 2 4 1 7 8 9 . Also …

Remove all occurences of an element from vector in O(n) complexityRead More

C++ std::vector example and why should I use std::vector?

Vector is a template based container that behaves just like a Dynamic Array. It can expands its memory at run time and always store elements in contiguous memory location just like Array. Advertisements We can store any type of element in vector by specifying the type as template argument. [showads ad=inside_post] Frequently Asked: Be careful …

C++ std::vector example and why should I use std::vector?Read More

How does std::vector works internally ?

std::vector allocates a memory on heap and store all its elements in contiguous memory location. But what if memory it allocated initially is completely filled? For example, let’s create a vector of ints i.e. std::vector<int> . Now suppose it’s initial capacity is to store 10 elements, but in our application we want to store 15 …

How does std::vector works internally ?Read More

Importance of Constructors while using User Defined Objects with std::vector

For User Defined classes if Copy Constructor and Assignment Operator are public then only one can insert it’s object in std::vector. This is because of two reasons, Advertisements All STL contains always stores the copy of inserted objects not the actual one. So, whenever we insert any element or object in    container then it’s …

Importance of Constructors while using User Defined Objects with std::vectorRead More

std::for_each Tutorial : Usage Details with Examples

The std::for_each() algorithm allows us to iterate over a given range of elements and perform operations over them.When to use std::for_each() ? Whenever you are iterating over the elements in a array or in a container and performing some different opeartions over them like accessing, modifying etc. Although you can achieve the same by typing …

std::for_each Tutorial : Usage Details with ExamplesRead More

Set vs Map : How to choose a right associative container ?

In this article we will discuss how set and map are different and what factors we need to keep in mind while choosing right associative container. Let’s start from set, Set : Advertisements Set is an associative container which we need to store unique elements. It always keeps the elements in sorted order. Internally it …

Set vs Map : How to choose a right associative container ?Read More

Cart

Your Cart is Empty

Back To Shop