Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 2041 of 3363
173 Views
In this tutorial, we will be discussing a program to understand iterator invalidation in C++.While iterating over the elements of a container object, sometimes it can become invalidated if we don’t apply bound checks. This mainly occurs due to the change in the shape and size of the container object.Example Live Demo#include using namespace std; int main() { //declaring a vector vector v{1, 5, 10, 15, 20}; //changing vector during execution //which will cause bound invalidation for (auto it=v.begin();it!=v.end();it++) if ((*it) == 5) v.push_back(-1); for (auto ... Read More
278 Views
In this tutorial, we will be discussing a program to understand the internal details of std::sort() in C++.std::sort() function is used to sort down an array using a comparison of the elements. If we look at the in-depth functionality of std::sort() it uses IntroSort algorithm to sort the elements of a container object.Example Live Demo#include using namespace std; int main(){ int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; int n = sizeof(arr)/sizeof(arr[0]); sort(arr, arr+n); cout
2K+ Views
In this tutorial, we will be discussing a program to understand integer literal in C/C++ (Prefixes and suffixes).Integer literals are literals for integer values directly represented in the source code. Further, they are of two types −Prefixes − Prefixes denotes the base of the value. For example, 0x10 indicates hexadecimal value with 0x.Suffixes − Suffixes denotes the type of the value. For example, 8465484156155LL denotes a long long integer.Example Live Demo#include using namespace std; int main(){ //prefixes cout
3K+ Views
In this tutorial, we will be discussing a program to understand INT_MAX and INT_MIN in C/C++.INT_MIN and INT_MAX are macros that are defined to set the minimum and maximum value for a variable/element.Example Live Demo#include int main(){ printf("%d", INT_MAX); printf("%d", INT_MIN); return 0; }Output2147483647 -2147483648ApplicationCalculating MIN value in an arrayExample Live Demo#include //calculating minimum element in an array int compute_min(int arr[], int n){ int MIN = INT_MAX; for (int i = 0; i < n; i++) MIN = std::min(MIN, arr[i]); std::cout
832 Views
In this tutorial, we will be discussing a program to understand the insertion sort using C++ STL.In this we use std::upper_bound to find the element at the wrong position, then rotate the unsorted part of the array to make it sorted.Example Live Demo#include //function to perform insertion sort void insertionSort(std::vector &vec){ for (auto it = vec.begin(); it != vec.end(); it++){ auto const insertion_point = std::upper_bound(vec.begin(), it, *it); std::rotate(insertion_point, it, it+1); } } //printing the array void print(std::vector vec){ for( int x : vec) std::cout
481 Views
In this tutorial, we will be discussing a program to understand the insertion and deletion in STL set in C++.The set is a container element. The properties that make it unique is that it can only contain unique elements and they can be looped over in a sorted manner.ExampleInsertion Live Demo#include #include using namespace std; int main(){ set st; //declaring iterators set::iterator it = st.begin(); set::iterator it1, it2; pair< set::iterator,bool> ptr; //inserting a single element ptr = st.insert(20); if (ptr.second) cout
400 Views
In this tutorial, we will be discussing a program to understand input iterators in C++.Input iterators are one of the five iterators in STL being the weakest and simplest of all. They are mostly used in serial input operations where each value is read one and then the iterator moves to the next one.Example Live Demo#include #include using namespace std; int main(){ vector v1 = { 1, 2, 3, 4, 5 }; //declaring iterator vector::iterator i1; for (i1 = v1.begin(); i1 != v1.end(); ++i1) { //looping over elements via iterator cout
299 Views
MethodHandles class introduced in Java 7 version. This class primarily added some static methods to better the functionality, and falls into several categories like Lookup methods that help to create method handles for methods and fields, Combinator methods that combine or transform pre-existing method handles into new ones, and factory methods to create method handles that emulate other common JVM operations or control flow patterns. MethodHandles class has enhanced in Java 9 to introduce many changes and added new static methods like arrayLength(), arrayConstructor(), zero(), and etc.Syntaxpublic class MethodHandles extends ObjectExampleimport java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; public class MethodHandlesTest { public void MethodHandle1() { ... Read More
232 Views
StackWalker API is a new feature in Java 9, and it improves the performance of the predecessor stack track element. It can also provide a way to filter the stack elements in case of exception or to understand application behavior. In Java 9, the way to access the stack trace is very limited and provide the entire stack information at once.In the below example, we need to print all attributes in Stack Frame Exampleimport java.lang.StackWalker.StackFrame; import java.util.*; import java.util.stream.*; import java.lang.StackWalker.Option; public class AllAttributesTest { public static void main(String args[]) { System.out.println("Java 9 Stack Walker API - Print all ... Read More
1K+ Views
Thread.onSpinWait() method has been introduced in Java 9. It is a static method of Thread class and can be optionally called in busy-waiting loops. It allows the JVM to issue processor instructions on some system architectures to improve reaction time in such spin-wait loops, and also reduce the power consumed by the core thread. It can benefit the overall power consumption of a java program and allows other core threads to execute at faster speeds within the same power consumption envelope.Syntaxpublic static void onSpinWait()Examplepublic class ThreadOnSpinWaitTest { public static void main(final String args[]) throws InterruptedException { ... Read More