Found 26504 Articles for Server Side Programming

upper_bound in C++

Arnab Chakraborty
Updated on 30-Dec-2019 10:02:52

146 Views

Here we will see that is the upper_bound() function in C++ STL. This function returns an iterator that points to the first element in the container, which is considered to go after val. The syntax is like:iterator upper_bound (const value_type& val); const_iterator upper_bound (const value_type& val) const;The return value is an iterator, pointing to the first element in the container which is considered to go after val.Example Live Demo#include #include using namespace std; int main () {    set myset;    set::iterator itlow, itup;    for (int i = 1; i < 10; i++) myset.insert(i*10);    itup = myset.upper_bound ... Read More

Shuffle an Array using STL in C++

Arnab Chakraborty
Updated on 30-Dec-2019 10:00:31

786 Views

Here we will see the Shuffle and random_shuffle in C++. These functions are used to shuffle array elements in C++. We can use the vector also instead of arrays, the usage is similar. Let us see the random_shuffle() first. It is used to randomly rearrange the elements in range [left, right). This function randomly swaps the positions of each element with the position of some randomly chosen positions.We can provide some random generator function to tell which element will be taken in every case. If we do not provide some, it will use its own random generator function.Example Live Demo#include ... Read More

Setting up C++ Development Environment

Arnab Chakraborty
Updated on 30-Dec-2019 09:58:46

200 Views

Text EditorThis will be used to type your program. Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.Name and version of text editor can vary on different operating systems. For example, Notepad will be used on Windows and vim or vi can be used on windows as well as Linux, or UNIX.The files you create with your editor are called source files and for C++ they typically are named with the extension .cpp, .cp, or .c.A text editor should be in place to start your C++ programming.C++ CompilerThis is an actual C++ ... Read More

getline (string) in C++

Arnab Chakraborty
Updated on 30-Dec-2019 09:58:05

894 Views

It is used to extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character). The declaration is like:basic_istream& getline (char_type* s, streamsize n ); basic_istream& getline (char_type* s, streamsize n, char_type delim);The parameters are ‘s’ pointer to an array of characters, where the extracted characters are stored as a c_string. Next parameter is ‘n’ this is the maximum number of characters to write (including the terminating character). The third parameter is ‘delim’ ... Read More

Constructor Overloading in C++

Arnab Chakraborty
Updated on 30-Dec-2019 09:55:35

4K+ Views

As we know function overloading is one of the core feature of the object oriented languages. We can use the same name of the functions; whose parameter sets are different. Here we will see how to overload the constructors of C++ classes. The constructor overloading has few important concepts.Overloaded constructors must have the same name and different number of argumentsThe constructor is called based on the number and types of the arguments are passed.We have to pass the argument while creating objects, otherwise the constructor cannot understand which constructor will be called.Example Live Demo#include using namespace std; class Rect{ ... Read More

Cons of using the whole namespace in C++

Arnab Chakraborty
Updated on 30-Dec-2019 09:53:42

281 Views

In C++, we use different namespaces. We can also create our own namespaces. For example, generally, we use standard namespace called std. We write the syntax like:using namespace std;In the standard library, it contains common functionality you use in building your applications like containers, algorithms, etc. If names used by these were out in the open, for example, if they defined a queue class globally, you'd never be able to use the same name again without conflicts. So they created a namespace, std to contain this change.The using namespace statement just means that in the scope it is present, make ... Read More

Find the compatibility difference between two arrays in C++

Arnab Chakraborty
Updated on 30-Dec-2019 09:50:39

547 Views

Consider there are two friends and now they want to test their bonding. So they will check, how much compatible they are. Given the numbers n, numbered from 1..n. And they are asked to rank the numbers. They have to find the compatibility difference between them. The compatibility difference is basically the number of mismatches in the relative ranking of the same movie given by them. So if A = [3, 1, 2, 4, 5], and B = [3, 2, 4, 1, 5], then the output will be 2. The compatibility difference is 2, as first ranks movie 1 before ... Read More

Find the closest pair from two sorted arrays in c++

Arnab Chakraborty
Updated on 30-Dec-2019 09:47:51

501 Views

Suppose we have two sorted arrays and a number x, we have to find the pair whose sum is closest to x. And the pair has an element from each array. We have two arrays A1 [0..m-1] and A2 [0..n-1], and another value x. We have to find the pair A1[i] + A2[j] such that absolute value of (A1[i] + A2[j] – x) is minimum. So if A1 = [1, 4, 5, 7], and A2 = [10, 20, 30, 40], and x = 32, then output will be 1 and 30.We will start from left of A1 and right from ... Read More

How to parse a CSV file using PHP

AmitDiwan
Updated on 30-Dec-2019 06:56:12

564 Views

To parse a CSV file in PHP, the code is as follows. Under fopen(), set the path of the .csv file−Example$row_count = 1; if (($infile = fopen("path to .csv file", "r")) !== FALSE) {    while (($data_in_csv = fgetcsv($infile, 800, ", ")) !== FALSE) {       $data_count = count($data_in_csv);       echo " $data_count in line $row_count: ";       $row_count++;       for ($counter=0; $counter < $data_count; $counter++) {          echo $$data_in_csv[$counter] . "";       }    }    fclose(infile); }Code explanation − The file can be opened in reading ... Read More

Performance of FOR vs FOREACH in PHP

AmitDiwan
Updated on 30-Dec-2019 06:53:00

2K+ Views

The 'foreach' is slow in comparison to the 'for' loop. The foreach copies the array over which the iteration needs to be performed.For improved performance, the concept of references needs to be used. In addition to this, ‘foreach’ is easy to use.ExampleBelow is a simple code example − Live DemoOutputThis will produce the following output −This completed in 0.00058293342590332 seconds This completed in 0.00063300132751465 seconds This completed in 0.00023412704467773 seconds This completed in 0.00026583671569824 seconds

Advertisements