Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Code Snippets Code Examples

A list splicing example.

/* A list splicing example. */ #include <iostream> #include <list> #include <string> #include <algorithm> using namespace std; int main() { list<string> sentence; list<string> phrase; list<string>::iterator p; string s1[] = {"A", "B", ""}; string s2[] = {"C", "D", ""}; string s3[] = {"E", "F", "G.", ""}; string s4[] = {"A", "C,", "E", "G", ""}; int i; for(i = 0; s1[ i ] != ""; i++) sentence.push_back(s1[i]); for(i = 0; s2[ i ] != ""; i++) phrase.push_back(s2[ i ]); cout << "Original sentence:\n"; p = sentence.begin(); while(p != sentence.end()) cout << *p++ << " "; cout << endl; sentence.splice(sentence.begin(), phrase); cout << "Sentence after splicing at the front:\n"; p = sentence.begin(); while(p != sentence.end()) cout << *p++ << " "; cout << endl; for(i = 0; s3[ i ] != ""; i++) phrase.push_back(s3[ i ]); sentence.splice(sentence.end(), phrase); cout << "Sentence after splicing at the end:\n"; p = sentence.begin(); while(p != sentence.end()) cout << *p++ << " "; cout << endl; for(i = 0; s4[ i ] != ""; i++) phrase.push_back(s4[ i ]); p = find(sentence.begin(), sentence.end(), "or"); sentence.splice(p, phrase); cout << "Sentence after splicing in the middle:\n"; p = sentence.begin(); while(p != sentence.end()) cout << *p++ << " "; return 0; }

List is a popularly used sequence container. Container is an object that holds data of same type. List container is implemented as doubly linked-list, hence it provides bidirectional sequential access to it's data. List doesn't provide fast random access, it only supports sequential access in both directions. List allows insertion and deletion operation anywhere within a sequence in constant time. Elements of list can be scattered in different chunks of memory. Container stores necessary information to allow sequential access to it's data. Lists can shrink or expand as needed from both ends at run time. The storage requirement is fulfilled automatically by internal allocator. Zero sized lists are also valid. In that case list.begin() and list.end() points to same location. But behavior of calling front() or back() is undefined. To define the std::list, we have to import the <list> header file.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

Add element at the end. Adds a new element at the end of the list container, after its current last element. The content of val is copied (or moved) to the new element. This effectively increases the container size by one. The list:push_back() function in C++ STL is used to add a new element to an existing list container. It takes the element to be added as a parameter and adds it to the list container. This function accepts a single parameter which is mandatory value. This refers to the element needed to be added to the list, list_name. This function does not return any value.

Return iterator to beginning. Returns an iterator pointing to the first element in the list container. Notice that, unlike member list::front, which returns a reference to the first element, this function returns a bidirectional iterator pointing to it. If the container is empty, the returned iterator value shall not be dereferenced. begin() function is used to return an iterator pointing to the first element of the list container. It is different from the front() function because the front function returns a reference to the first element of the container but begin() function returns a bidirectional iterator to the first element of the container. This function does not accept any parameter. Function returns an iterator to the beginning of the sequence container.

Iterators are just like pointers used to access the container elements. Iterators are one of the four pillars of the Standard Template Library or STL in C++. An iterator is used to point to the memory address of the STL container classes. For better understanding, you can relate them with a pointer, to some extent. Iterators act as a bridge that connects algorithms to STL containers and allows the modifications of the data present inside the container. They allow you to iterate over the container, access and assign the values, and run different operators over them, to get the desired result. • Iterators are used to traverse from one element to another element, a process is known as iterating through the container. • The main advantage of an iterator is to provide a common interface for all the containers type. • Iterators make the algorithm independent of the type of the container used.

Relational operators for string. Performs the appropriate comparison operation between the string objects lhs and rhs. The functions use string::compare for the comparison. These operators are overloaded in header <string>. If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.

Consider a situation, when we have two persons with the same name, jhon, 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 you are referring to within your code.

Transfer elements from list to list. Transfers elements from x into the container, inserting them at position. The list::splice() is a built-in function in C++ STL which is used to transfer elements from one list to another. This effectively inserts those elements into the container and removes them from x, altering the sizes of both containers. The operation does not involve the construction or destruction of any element. They are transferred, no matter whether x is an lvalue or an rvalue, or whether the value_type supports move-construction or not. The first version (1) transfers all the elements of x into the container. The second version (2) transfers only the element pointed by i from x into the container. The third version (3) transfers the range [first,last) from x into the container.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

C++ find() function is part of the standard library function which tries to find the first occurrence of the specified range of element where the range starts with first range to last range and that iterator encounters the first element, compares for the value which must be equal after all possible comparisons and if no element is found it returns the last element. For making all the comparisons it makes use of the operator = for comparison. If find() function performs any unnecessary action it throws exceptions that are not required by the programmer. Find value in range. Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last. The function uses operator== to compare the individual elements to val.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

Return iterator to end. Returns an iterator referring to the past-the-end element in the list container. The past-the-end element is the theoretical element that would follow the last element in the list container. It does not point to any element, and thus shall not be dereferenced. Because the ranges used by functions of the standard library do not include the element pointed by their closing iterator, this function is often used in combination with list::begin to specify a range including all the elements in the container. If the container is empty, this function returns the same as list::begin. This function does not accept any parameter.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

C++ program performs Naive string matching without using any specific library functions. A text and a pattern is given as input. Pattern is 'searched' for in the text and all "instances" of

C++ program performs Naive string matching without using any specific library functions. A text and a Pattern is given as input. Pattern is searched for in a text and all 'instances' of the