Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
find() in C++ STL
Next article icon

std::next in C++

Last Updated : 02 Aug, 2017
Comments
Improve
Suggest changes
Like Article
Like
Report

std::next returns an iterator pointing to the element after being advanced by certain no. of positions. It is defined inside the header file .

It does not modify its arguments and returns a copy of the argument advanced by the specified amount. If it is a random-access iterator, the function uses just once operator + or operator – for advancing. Otherwise, the function uses repeatedly the increase or decrease operator (operator ++ or operator –) on the copied iterator until n elements have been advanced.

Syntax:

  ForwardIterator next (ForwardIterator it,         typename iterator_traits::difference_type n = 1);  it: Iterator to the base position.  difference_type: It is the numerical type that represents   distances between iterators of the ForwardIterator type.  n: Total no. of positions by which the  iterator has to be advanced. In the syntax, n is assigned  a default value 1 so it will atleast advance by 1 position.    Returns: It returns an iterator to the element   n positions away from it.    




 
 

Output:

  v1 = 1 2 3 4 5 6 7  v2 = 8 9 10 1 2 3 4  

How can it be helpful ?

  • Advancing iterator in Lists: Since, lists support bidirectional iterators, which can be incremented only by using ++ and – – operator. So, if we want to advance the iterator by more than one position, then using std::next can be extremely useful.




    // C++ program to demonstrate std::next
    #include <iostream>
    #include <iterator>
    #include <list>
    #include <algorithm>
    using namespace std;
    int main()
    {
        // Declaring first container
        list<int> v1 = { 1, 2, 3, 7, 8, 9 };
      
        // Declaring second container
        list<int> v2 = { 4, 5, 6 };
      
        list<int>::iterator i1;
        i1 = v1.begin();
        // i1 points to 1 in v1
      
        list<int>::iterator i2;
        // i2 = v1.begin() + 3;
        // This cannot be used with lists
        // so use std::next for this
      
        i2 = std::next(i1, 3);
      
        // Using std::copy
        std::copy(i1, i2, std::back_inserter(v2));
        // v2 now contains 4 5 6 1 2 3
      
        // Displaying v1 and v2
        cout << "v1 = ";
      
        int i;
        for (i1 = v1.begin(); i1 != v1.end(); ++i1) {
            cout << *i1 << " ";
        }
      
        cout << "\nv2 = ";
        for (i1 = v2.begin(); i1 != v2.end(); ++i1) {
            cout << *i1 << " ";
        }
      
        return 0;
    }
     
     

    Output:

      v1 = 1 2 3 7 8 9  v2 = 4 5 6 1 2 3    

    Explanation: Here, just look how if we want copy only a selected portion of the list, then we can make use of std::next, as otherwise we cannot use any +=, -= operators with bidirectional iterators supported by lists. So, we used std::next and directly advanced the iterator by three positions.



Next Article
find() in C++ STL
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • C++
  • Misc
  • cpp-iterator
  • STL
Practice Tags :
  • CPP
  • Misc
  • STL

Similar Reads

  • std::inserter in C++
    std::inserter constructs an insert iterator that inserts new elements into x in successive locations starting at the position pointed by it. It is defined inside the header file . An insert iterator is a special type of output iterator designed to allow algorithms that usually overwrite elements (su
    4 min read
  • std::move in C++
    std :: move Moves the elements in the range [first,last] into the range beginning at the result. The value of the elements in the [first,last] is transferred to the elements pointed out by the result. After the call, the elements in the range [first,last] are left in an unspecified but valid state.
    2 min read
  • std::prev in C++
    std::prev returns an iterator pointing to the element after being advanced by certain number of positions in the reverse direction. It is defined inside the header file iterator. It returns a copy of the argument advanced by the specified amount in the backward direction. If it is a random-access it
    5 min read
  • std::search in C++
    std::search is defined in the header file <algorithm> and used to find out the presence of a subsequence satisfying a condition (equality if no such predicate is defined) with respect to another sequence. It searches the sequence [first1, last1) for the first occurrence of the subsequence defi
    4 min read
  • find() in C++ STL
    C++ find() is a built-in function used to find the first occurrence of an element in the given range. It works with any container that supports iterators, such as arrays, vectors, lists, and more. In this article, we will learn about find() function in C++. [GFGTABS] C++ //Driver Code Starts{ #inclu
    2 min read
  • List in C++ STL
    In C++, list container implements a doubly linked list in which each element contains the address of next and previous element in the list. It stores data in non-contiguous memory, hence providing fast insertion and deletion once the position of the element is known. Example: [GFGTABS] C++ #include
    8 min read
  • sort() in C++ STL
    In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays. Let's take a look at an example: [GFGTABS]
    4 min read
  • map insert() in C++ STL
    The std::map::insert() is a built-in function of C++ STL map container which is used to insert new elements into the map. In this article, we will learn how to use map::insert() function in our C++ programs. Example: [GFGTABS] C++ // C++ program to illustrate how to use // map::insert #include <b
    5 min read
  • std::back_inserter in C++
    std::back_inserter constructs a back-insert iterator that inserts new elements at the end of the container to which it is applied. It is defined inside the header file . A back-insert iterator is a special type of output iterator designed to allow algorithms that usually overwrite elements (such as
    4 min read
  • std::string::insert() in C++
    In C++, the string::insert() function is used insert the characters or a string at the given position of the string. It is the member function of std::string class. The string::insert method can be used in the following ways to: Table of Content Insert a Single CharacterInsert a Single Character Mul
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences