Found 7401 Articles for C++

Command line arguments in C/C++

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

4K+ Views

It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied ... Read More

Lambda expression in C++

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

376 Views

C++ STL includes useful generic functions like std::for_each. Unfortunately they can also be quite cumbersome to use, particularly if the functor you would like to apply is unique to the particular function. So this function that you'll create will be in that namespace just being used at that one place. The solution to this is using anonymous functions.C++ has introduced lambda expressions in C++11 to allow creating anonymous function. For example, Example Live Demo#include #include #include // for_each using namespace std; int main() {    vector myvector;    myvector.push_back(1);    myvector.push_back(2);    myvector.push_back(3);    for_each(myvector.begin(), myvector.end(), [](int x) {   ... Read More

Zombie and Orphan Processes in Linux

George John
Updated on 30-Jul-2019 22:30:25

6K+ Views

Details about the zombie, orphan and daemon processes are given as followsZombie ProcessesA zombie process is a process whose execution is completed but it still has an entry in the process table. Zombie processes usually occur for child processes, as the parent process still needs to read its child’s exit status. Once this is done using the wait system call, the zombie process is eliminated from the process table. This is known as reaping the zombie process.A diagram that demonstrates the creation and termination of a zombie process is given as followsZombie processes don't use any system resources but they ... Read More

Copy-and-Swap Idiom in C++

Chandu yadav
Updated on 30-Jul-2019 22:30:25

185 Views

The assignment consists of 2 steps, tearing an object's old state and building a new state for it. The destructor is used for the first step and a copy constructor does the second step.Implementing both of these is straightforward. But when overloading the assignment operator, it can become quite difficult to implement. The copy and swap idiom is a solution for the same.This idiom uses the copy-constructor to build a local copy of the data. It then swaps the old data with the new data using the swap function. The temporary copy is then destructed using the destructor. We finally ... Read More

Type Inference in C++

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

232 Views

Type inference or deduction refers to the automatic detection of the data type of an expression in a programming language. It is a feature present in some strongly statically typed languages. In C++, the auto keyword(added in C++ 11) is used for automatic type deduction. For example, you want to create an iterator to iterate over a vector, you can simply use auto for that purpose.Example#include #include using namespace std; int main() {    vector arr(10);    for(auto it = arr.begin(); it != arr.end(); it ++) {       cin >> *it;    }    return 0; }OutputIn the ... Read More

Strand sort in C++

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

213 Views

In this section we will see how we can sort some array or linked list using standard library of C++. In C++ there are multiple different libraries that can be used for different purposes. The sorting is one of them.The C++ function std::list::sort() sorts the elements of the list in ascending order. The order of equal elements is preserved. It uses operator< for comparison.Example Live Demo#include #include using namespace std; int main(void) {    list l = {1, 4, 2, 5, 3};    cout

Sorting in C++

George John
Updated on 30-Jul-2019 22:30:25

1K+ Views

In this section we will see how to perform sorting algorithm in C++. A sorted array is an array in which each of the elements are sorted in some order such as numerical, alphabetical etc. There are many algorithms to sort a numerical array such as bubble sort, insertion sort, selection sort, merge sort, quick sort, heap sort etc. More details about sorting the array using selection sort are given below.The selection sort is a sorting method that yields a sorted array. It does so by repeatedly finding the smallest element in the array and interchanging it with the element ... Read More

Can namespaces be nested in C++?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

148 Views

Yes the namespace can be nested in C++. We can define one namespace inside another name space as follows −Syntaxnamespace namespace_name1 {    // code declarations    namespace namespace_name2 {       // code declarations    } }You can access members of nested namespace by using resolution operators as follows −// to access members of namespace_name2 using namespace namespace_name1::namespace_name2; // to access members of namespace:name1 using namespace namespace_name1;Example Live Demo#include using namespace std; // first name space namespace first_space {    void func() {       cout

Namespace in C++

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

183 Views

Consider a situation, when we have two persons with the same name, Zara, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother’s or father’s name, etc.Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function ... Read More

C++ default constructor

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

198 Views

A class constructor is a special member function of a class that is executed whenever we create new objects of that class.A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.Following example explains the concept of constructor −Example Live Demo#include using namespace std; class Line {    public:       void setLength( double len );       double getLength( void );       Line(); // This is the constructor       private:       double length; }; // Member functions definitions including constructor Line::Line(void) {    cout

Advertisements