TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm copy_backward() function

C++ algorithm copy_backward() function tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm Functions copy_backward()

C++ Algorithm copy_backward() function is used for copying of elements in the backward order, it accepts three arguments and then copies the elements belonging to the range [first,last]. The copying of elements begins in the reverse order with termination point at 'result'.

Syntax

template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);

Parameter

first: It is a bidirectional iterator to the first element of the range, where the element itself is included in the range.

last: It is a bidirectional iterator to the last element of the range, where the element itself is not included in the range.

result: It is a bidirectional iterator to the final position of copied elements.

Return value

The function returns an iterator of the first element to the sequence of copied ones.

Example 1

#include <iostream>     
#include <algorithm>    
#include <vector>       
int main () 
{ std::vector<int> newvector;
  for (int k=1; k<=5; k++)
  newvector.push_back(k*5);          
  newvector.resize(newvector.size()+3);  
  std::copy_backward ( newvector.begin(), newvector.begin()+5, newvector.end() );
  std::cout << "newvector contains:";
  for (std::vector<int>::iterator ti=newvector.begin(); ti!=newvector.end(); ++ti)
  std::cout << ' ' << *ti;
  std::cout << '\n';
  return 0;
}

Output:

newvector contains: 5 10 15 5 10 15 20 25

Complexity

The complexity of the function is linear starting from the first element to the last one.

Data races

Some or all of the container objects are accessed.

Exceptions

The function throws an exception if any of the container elements throws one.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf