神秘博客

C++ Primer Plus [第六版]第4章复习题答案

13

#include <iostream>
#include <vector>

int main()
{
  using namespace std;
  int n;
  
  cout << "请输入一个整数: ";
  cin >> n;
  int *pt = new int[n];
  pt[0] = n;
  cout << "new 创建动态数组完成\n" << "pt[0] = " << pt[0] << endl;
  delete[]pt;
  vector<int> m(n);
  m[0] = n;
  cout << "vector 创建动态数组完成\n" << "m[0] = " << m[0] << endl;

  system("pause");
  return 0;
}

15

#include <iostream>
#include <cstring>

struct fish
{
  char type[20];
  int weight;
  float length;
};

int main()
{
  using namespace std;
  fish *list = new fish;
  strcpy_s(list->type, 20, "鲫鱼");
  list->weight = 45;
  list->length = 3.5;

  cout << "种类 重量[盎司] 长度[英寸]\n";
  cout << list->type << " " << list->weight << "         " << list->length << endl;

  delete list;
  system("pause");
  return 0;
}

 

版权说明:
点赞
  1. admin说道:

    hello world
    :razz:

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏