Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 9:40 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: count elements in a vector
PostPosted: Thu Nov 13, 2008 7:33 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

Code:
#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>

int main()
{
   std::ostream_iterator< int > output( cout, " " );

   int a2[ 10 ] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 };
   std::vector< int > v2( a2, a2 + 10 ); // copy of a2
   cout << "Vector v2 contains: ";
   std::copy( v2.begin(), v2.end(), output );

   // count number of elements in v2 with value 8
   int result = std::count( v2.begin(), v2.end(), 8 );
   cout << "\nNumber of elements matching 8: " << result;

   cout << endl;
   return 0;
}

/*
Vector v2 contains: 100 2 8 1 50 3 8 8 9 10
Number of elements matching 8: 3

*/


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 lesson6: XSD Complex Empty Elements  XSD Tutorials  msi_333  1
 count the number of files in directory & its sub directories  PHP examples  msi_333  0
 how to load the form elements depending on selection option  Scripting Language  saics  16
 Data Structures: Vector  Java examples  isuru  0
 Elements of a Java Program  Java  msi_333  2

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team