Foreach in C++ and JAVA28 Aug 2024 | 4 min read The foreach loop is used to quickly iterate over the elements of a container (array, vectors, etc.) without performing initialization, testing, or increment/decrement. Foreach loops work by doing something for each element rather than doing something n times. Although there is no foreach loop in C, it is supported by C++ and Java. It was first introduced in C++ in C++ 11, and in Java in JDK 1.5.0. In both C++ and Java, the keyword for foreach loop is "for." SyntaxWe no longer need to specify the data type for variables in foreach loops thanks to the introduction of the auto keyword in C++ and the var keyword in Java. Type inference detects the data type of the container and sets the variable used for traversing to the same data type. The code below demonstrates the use of a foreach loop for various containers, as well as the auto/var keywords in C++/Java. C++JAVAOutput Traversing the array with foreach using array's data type: 10 20 30 40 Traversing the array with foreach using auto keyword : 10 20 30 40 Vector C++ programme:Output Traversing the vector with foreach using vector's data type: This is foreach example using vector. Traversing the vector with foreach using auto keyword : This is foreach example using vector. C++/Java Set Program:C++JAVAOutput Traversing the set with foreach using set's data type: 1 2 4 5 6 7 10 Traversing the set with foreach using auto keyword : 1 2 4 5 6 7 10 For array, vector, and set, we can use different data types in foreach. C++/Java Map Program:C++JAVAOutput Traversing the map with foreach using map's data type 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example Traversing the map with foreach using auto keyword 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example Foreach loop has the following advantages:
Foreach loop has the following disadvantage:
Next TopicC++ templates vs. Java generics |
PAIR IN C++
Pair is a term used to combine two values, which may be of various data kinds. Pair offers a technique to keep two disparate objects together in storage. Essentially, we use it to store tuples. A pair container is a straightforward container that is specified in...
3 min read
Trie Data Structure in C++
In this article, we will discuss the trie data structure in C++ with its properties, operations, and examples. Trie data structure is a type of multi-way tree that is used for storing different strings. Each string consists of characters that are stored in a tree-like structure, i.e.,...
8 min read
Count Smaller Elements on Right Side in C++
In this article, we will discuss how to count smaller elements on the right side in C++ with several examples. Below is the N-dimensional unsorted array arr[], which is made up of unique integers. Our objective is to create a second array count where the count will...
9 min read
Static Polymorphism in C++
Polymorphism is a fundamental concept in object-oriented programming, allowing different types of objects to be treated as though they have a single type. The two main methods used to implement the polymorphism are static and dynamic polymorphism. This discussion centers on static polymorphism, a potent tool...
4 min read
Chrono in C++
Chrono is a C++ header that contains a set of time-related classes and methods. It is part of the C++ Standard Template Library (STL) and is included in C++11 along with subsequent versions. <Chrono> supports three sorts of clocks: system_clock, steady_clock, and high_resolution_clock. These clocks are utilized...
3 min read
StringStream in C++ for Decimal to Hexadecimal and Hexadecimal to Decimal
In this article, you will learn about the . But before discussing its implementation, you must know about the stringStream in C++. What is the stringstream in C++? A strong feature in C++ called StringStream enables smooth conversion between various data types and string representations. StringStream makes handling...
4 min read
One time pad algorithm in C++
This C++ application uses the one-time pad cipher technique to encrypt any message. The input is case-insensitive and compatible with all characters. In the decrypted message, white spaces are generated as random characters rather than being disregarded. Example: The C++ program's source code for implementing the one-time pad...
3 min read
Create Linked List In C++
What is a linked list? A linked list is a linear data structure that consists of a sequence of nodes, where each node stores a piece of data and a reference (pointer) to the node in the List. Linked lists are useful for storing data collections...
6 min read
Sort elements by frequency in C++
When sorting elements in C++, the frequency of each element is counted and then used to determine the order in which the elements should be sorted. You can complete this work by utilizing sorting algorithms like std::sort as well as data structures like std::map and std::unordered_map. Informational...
3 min read
Activity Selection Problem in C++
Activity Selection is a classical problem in computer science, which can be solved using greedy algorithms. In this problem, we are given a set of activities that are to be performed during a given period, and each activity has a start time and an end time....
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India