Essential C++的例子学习

2008-04-07 16:54:44 来源:中国自学编程网 作者:佚名

#include <vector>
#include <string>
#include <iostream>
//#include <cstdlib>
using namespace std;

int main()
{
 int elem_vals[18]={1,2,3,3,4,7,2,5,12,3,6,10,4,9,16,5,12,22};
 vector<int> fibonacci(elem_vals,elem_vals+3);
 vector<int> lucas(elem_vals+3,elem_vals+6);
 vector<int> pell(elem_vals+6,elem_vals+9);
 vector<int> trigangular(elem_vals+9,elem_vals+12);
 vector<int> square(elem_vals+12,elem_vals+15);
 vector<int> pentagonal(elem_vals+15,elem_vals+18);
 vector<int> *seq_addr[6]={&fibonacci,&lucas,&pell,&trigangular,&square,&pentagonal};
 srand(6);
 int seq_index=rand()%6;
 vector<int> *curvector=seq_addr[seq_index];
 return 0;
}


//////////////////////////////////////////////////////////

#include <iostream>
#include <vector>
#include <conio.h>
#include <fstream>
using namespace std;
void display( vector<int>);
void swap( int&, int& );
void bubble_sort( vector<int>&,ofstream*);

int main()
{
    int ia[8]={8,34,3,13,1,21,5,2};
    vector<int> vec(ia,ia+8 );
 ofstream ofil("data.txt");
    cout<<"vector before sort:\t";
    display( vec );
    
    cout<<" vector after sorted:\t";
    bubble_sort( vec ,&ofil);
    display( vec );
    getch();
    return 0;
}


void display( vector<int> vec )
{
 for (int ix=0;ix<vec.size();++ix )
  cout<<vec[ix]<<’ ’;
 cout<<endl;
}

void swap( int &val1, int &val2 )
{
 int temp;
 temp = val1;
 val1 = val2;
 val2 = temp;
}


void bubble_sort( vector<int> &vec,ofstream *ofil=0)
{
 for (int ix=0;ix<vec.size();++ix)
  for (int jx=ix+1;jx<vec.size();++jx)
   if (vec[ix] > vec[jx])
   {
    *ofil<<"about to call swap ix="<<ix<<"jx="<<jx<<"\tswapping"
 <<vec[ix]<<"with"<<vec[jx]<<endl;
                swap( vec[ix], vec[jx]);
   }
}


////////////////////////


#include <iostream>
#include <vector>
#include <conio.h>
#include <fstream>
using namespace std;

void display( vector<int> vec )
{
 for (int ix=0;ix<vec.size();++ix )
  cout<<vec[ix]<<’ ’;
 cout<<endl;
}

void swap( int &val1, int &val2 )
{
 int temp;
 temp = val1;
 val1 = val2;
 va

9 7 3 1 2 3 4 4 8 :

本类最新行业评测技巧教程学院
本类热点本日本周本月
本类推荐本日本周本月