An array can use STL functions

vegaseat 3 Tallied Votes 342 Views Share

Nothing new, but something for the neophyte to ponder. Some of the functions (algorithms) used by STL containers can be applied to a normal array to do inane things like copying, displaying, finding max/min, reversing, replacing, searching, shuffling, sorting, summation. This can really simplify your code.

thekashyap commented: Decent collection of examples.. +8
// using STL container functions with arrays
// a Dev-C++ tested console application by  vegaseat  02jan2005

#include <iostream>
#include <algorithm>  // random_shuffle(), sort(), reverse() ...
#include <numeric>    // accumulate()
#include <iterator>   // ostream_iterator

using namespace std;

int main()
{
  // set up output for integers
  // display each element separated by a space
  ostream_iterator<int>  out(cout," ");

  // create an array of 10 integers
  int iA[10];

  // load the array
  for( int k = 0; k < 10; k++ )
  {
    iA[k] = k;
    //cout << iA[k] << endl;  // just a check
  }

  // set iterators to begin and end of the array
  // end does point one element past the loaded array
  // this is not an error!
  int * begin = &iA[0];
  int * end = &iA[10];

  // shuffle the array
  cout << "array after random_shuffle:\n";
  random_shuffle( begin, end );
  // iterate through the array and display
  copy( begin, end, out );
  cout << endl;

  // find max and min value
  int * m;
  m = max_element( begin, end);
  cout << "max value in array = " << *m << endl;
  m = min_element( begin, end);
  cout << "min value in array = " << *m << endl;

  // sort the array in ascending order
  cout << "array after sort:\n";
  sort( begin, end );
  copy( begin, end, out );
  cout << endl;

  // search for a specific value, here 7
  // needs sorted array!
  if (binary_search( begin, end, 7) == true)
  {
    cout << "found integer 7 in sorted array\n";
  }
  else
  {
    cout << "didn't find 7\n";
  }

  // reverse it
  cout << "sorted array in reverse:\n";
  reverse( begin, end );
  copy( begin, end, out );
  cout << endl;

  // replace any integer 3 with integer 8
  cout << "sorted again and replaced 3 with an 8:\n";
  sort( begin, end );
  replace( begin, end, 3, 8 );
  copy( begin, end, out );
  cout << endl;

  // replace any integers 8 with 3
  cout << "replaced (any) 8 with 3:\n";
  replace( begin, end, 8, 3 );
  copy( begin, end, out );
  cout << endl;

  // add up all the integer values (init = 0)
  int sum = accumulate( begin, end, 0);
  cout << "sum of integer elements in array = " << sum << endl;
  // copy array iA into array iB
  // note that begin and end are the iterators for array iA
  int iB[10] = {0};
  copy( begin, end, iB );
  cout << "copy array iA into array iB:" << endl;
  for( int k = 0; k < 10; k++ )
  {
    cout << iB[k] << endl;
  }
  cout << endl;

  // strings anyone?
  string sA[10];
  sA[0] = "Exact estimate";
  sA[1] = "Pretty ugly";
  sA[2] = "Diet ice cream";
  sA[3] = "Tight slacks";
  sA[4] = "Plastic glasses";
  sA[5] = "Butt Head";
  sA[6] = "Alone together";
  sA[7] = "Genuine imitation";
  sA[8] = "Found missing";
  sA[9] = "Business ethics";

  // set iterators to beginning and end of string array
  // notice that endstr goes one element past the loaded array
  string * beginstr = &sA[0];
  string * endstr = &sA[10];
  // change the output to handle strings and
  // display each string on a new line
  ostream_iterator<string>  outstr(cout,"\n");

  cout << "array as loaded:\n";
  copy( beginstr, endstr, outstr );
  cout << endl;

  // now for the fun of it ...
  // also, binary search needs a sorted array
  cout << "array after sort:\n";
  sort( beginstr, endstr );
  copy( beginstr, endstr, outstr );
  cout << endl;

  // you get the drift ...
  if (binary_search( beginstr, endstr, "Butt Head") == true)
  {
    cout << "found Butt Head in sorted array\n";
  }
  else
  {
    cout << "didn't find Butt Head\n";
  }
  cout << endl;

  cout << "let's reshuffle the strings:\n";
  // for those of you who want to simplify the code, but make 
  // it somewhat harder to read, you can imply the iterators
  //   random_shuffle( beginstr, endstr );
  //   copy( beginstr, endstr, outstr );
  // now becomes ...
  random_shuffle( sA, sA+10 );
  copy( sA, sA+10, outstr );
  cout << endl;
  
  // a simple search of an integer array
  int a[12] = {1, 2, 5, 7, 9, 8, 7, 3, 9, 8, 5, 7};
  int b[2] = {9, 8};  // search for b in a
  int *p1, *p2;

  p1 = search(a, a + 12, b, b + 2);    // get first match
  p2 = find_end(a, a + 12, b, b + 2);  // get last match
  cout << "looking for {9, 8} in {1, 2, 5, 7, 9, 8, 7, 3, 9, 8, 5, 7}:\n";
  cout << "search() index = " << p1 - a << "  find_end() index = " << p2 - a << endl;  

  cin.get();  // make console display wait for key press
  return 0;   // in the newer versions of C++ return is optional
}
Rashakil Fol 978 Super Senior Demiposter Team Colleague

Instead of &iA[0] and &iA[10], why not just write iA and (iA + 10)?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I do that at the end of the snippet as another option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.