ch5 指针、数组和结构

5 .10 定义一个字符串数组,其中字符串保存的是月份的名字。打印这些字符串。将字符串传递给一个函数打印这些字符串。
//例子 1
include <iostream>
#include <algorithm>
#include <string>
#include <iterator>

// If your compiler does not yet place its standard library in
// namespace 'std', omit the following four lines.
using std::copy;
using std::cout;
using std::ostream_iterator;
using std::string;

int const n_months = 12;

string month[n_months]
    = { string("January"), string("February"),
        string("March"), string("April"),
        string("May"), string("June"),
        string("July"), string("August"),
        string("September"), string("October"),
        string("November"), string("December") };

int main() {
   copy(&month[0], &month[0]+n_months,
         ostream_iterator<string>(cout, "\n"));
   return 0;
}

//例子2
#include <iostream>

int const n_months = 12;

char const *month[n_months]
   = {"January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November",
      "December"};

int main() {
   for (int k = 0; k!=n_months; ++k)
      std::cout << month[k] << '\n';
   return 0;
}

【相关文档】
1. istream_iterator、ostream_iterator 使用初探
2.copy


5.11 从输入读入一系列的单词,使用Quit作为输入的结束词。按照读入的顺序打印出这些单词。但是同一个单词不要打印两次。修改这个程序。做打印之前对单词排序。

#include <algorithm>
#include <iostream>
#include <list>
#include <set>
#include <string>
#include <iterator>
int main() {
   using std::copy;
   using std::cout;
   using std::ostream_iterator;
   using std::pair;
   using std::string;
   typedef std::set<string> WordSet;
   typedef WordSet::iterator WordIter;
   typedef std::list<WordIter> Index;
   WordSet words;
   Index input_order;
   // Input the words uniquely
   //循环输入数据,遇到Quit退出
   for(string new_word;
       cin >> new_word, new_word!="Quit";) {
   // 返回值类型为pair 
      pair<WordIter, bool> const &trace = words.insert(new_word);
      if (trace.second)
         input_order.push_back(trace.first);
   }
   // Output unique words in order of input:
   cout << "\n>>> Unique words in input order:\n";
   for (Index::iterator p = input_order.begin(); p!=input_order.end(); ++p) {
      cout << *(*p) << '\n';
   }
   // Output unique words in default set<string> order:
   cout << "\n>>> Unique words in sorted order:\n";
   //set 类型 树形结构,有序
   copy(words.begin(), words.end(),
        ostream_iterator<string>(cout, "\n"));
   return 0;
}

【相关文档】
1,std::pair
2.c++学习:pair的使用
3.STL中set的insert操作的返回值
4.set


5.12 写一个函数,统计在一个string里的一对字母出现的次数。写另一个函数对已0结束的char的数组做同样的事情。
#include <assert.h>
#include <stdio.h>
#include <string>
//using std::string;

size_t count_charpair(string const &s, char a, char b) {
   size_t result = 0;
   string::const_iterator p = s.begin();
   while(p!=s.end())
      if (*p++==a)
         if (p!=s.end() && *p==b)
            ++result;
   return result;
}

size_t count_charpair(char const *s, char a, char b) {
   assert(s);
   size_t result = 0;
   while (*s)
      if (*s++==a)
         if (*s==b)
            ++result;
   return result;
}

int main() {
   printf("Pairs ab in \"aabbaba\": %d\n",
          count_charpair("aabbaba", 'a', 'b'));
   printf("Pairs ab in string(\"aabbaba\"): %d\n",
          count_charpair(string("aabbaba"), 'a', 'b'));
   return 0;
}


5.13 定义一个struct Date 以保存日期的轨迹,提供一些函数,从读入到读Date,向输出写Date,以及用一个日期去初始化Date.
#include <iostream>
#include <stdlib.h>

struct Date {
   unsigned day_: 5; // Bitfield: use only 5 bits to
                    //  represent this integer member
   unsigned month_: 4;
   int year_: 15;
};

std::istream& operator>>(std::istream &input, Date &d) {
   int const bufsize = 6;
   char buffer[bufsize];
   input.getline(buffer, bufsize, '/');
   d.month_ = atoi(buffer);
   input.getline(buffer, bufsize, '/');
   d.day_ = atoi(buffer);
   int y;
   input >> y;
   d.year_ = y; 
   return input;
}

std::ostream& operator<<(std::ostream &output, Date &d) {
   output << d.month_ << '/' << d.day_ << '/' << d.year_;
   return output;
}

Date& init(Date &d, unsigned day, unsigned month, int year) {
   d.day_ = day;
   d.month_ = month;
   d.year_ = year;
   return d;
}

【这个没看懂。。。。】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值