设为首页 加入收藏

TOP

C++标准库map类型(一)
2013-01-01 14:51:14 】 浏览:1113
Tags:标准 map 类型

  1、定义一个指针类型

  如:typedef   double*   DoubleArrayPtr;

  2、声明一个指针变量

  如: DoubleArrayPtr   a;

  3、调用new

  a =  new   double[ array_size ];

  4:、动态数组的长度在方括号中给出

  5、像普通数组那样使用

  6、调用delete [ ]

  如:delete  [ ]   a;

  [cpp]

  // String.cpp : Defines the entry point for the console application.

  //

  #include "stdafx.h"

  #include<iostream>

  #include<cstdlib>

  #include<cstddef>///******

  typedef int* IntArrayPtr;  ///(1)

  void fill_array(int a[] , int size);

  void sort(int a[] , int size);

  int main(int argc, char* argv[])

  {

  using namespace std;

  cout《"This program sorts numbers from highest to lowest .\n";

  int array_size;

  cout《"How many numbers will be sorted \n";

  cin》array_size;

  IntArrayPtr a;///(2)

  ///创建动态数组

  a = new int[array_size]; ///(3)

  fill_array(a, array_size);

  sort(a, array_size);

  cout《"In sorted order the numbers are :\n";

  for(int index = 0; index < array_size ; index++)

  {

  cout《a[index]《" ";

  }

  cout《endl;

  delete [] a;  //(5)

  return 0;

  }

  void fill_array(int a[] ,int size)

  {

  using namespace std;

  cout《"Enter "《size《" integers.\n";

  for (int index = 0; index < size ;index++)

  cin》a[index];

  }

  void sort(int a[] , int size)

  {

  int i , j ;

  //选择排序

  ///将数组降序排序

  for(i = 0; i < size - 1 ; i++)

  for( j = i +1; j < size ; j++)

  {

  if( a[i] < a[j] )

  {

  int temp = a[i];

  a[i] = a[j];

  a[j]  = temp;

  }

  }

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇The Hamming Distance&.. 下一篇c/c++项目如何管理头文件

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目