【相关文档】
1.size_t、ptrdiff_t
#include "stdafx.h"
#include <iostream>
#include<cassert>
#include<cstddef>
#include <cstdlib> //for rand()
using namespace std;
class Histogram{
public:
Histogram(ptrdiff_t minval, size_t gap, size_t n_bins);
~Histogram(){ delete[] bin_; }
void record(ptrdiff_t);
void output_to(ostream&);
private:
ptrdiff_t const minval_, maxval_; //最小,最大边界
size_t const gap_; //区间数
size_t * const bin_; //存储区
size_t n_samll_, n_large_;
};
Histogram::Histogram(ptrdiff_t minval, size_t gap, size_t n_bins):minval_(minval),
maxval_(minval+n_bins*gap-1),gap_(gap),bin_(new size_t[n_bins]),n_samll_(0),n_large_(0){
assert(gap!=0&&n_bins!=0);
for (int k = 0; k != n_bins; ++k){
bin_[k] = 0;
}
}
void Histogram::record(ptrdiff_t datapoint){
if (datapoint < minval_){
++n_samll_;
}
else if (datapoint>maxval_){
++n_large_;
}
else
++bin_[(datapoint - minval_) / gap_];
}
void Histogram::output_to(ostream& o){
o << "< " << minval_ << ": "<< n_samll_ << endl;
for (ptrdiff_t left = minval_; left < maxval_; left += gap_){
o << left << "..." << left + gap_ - 1 << " : " << bin_[(left - minval_) / gap_] << endl;
}
o << " > " << maxval_ << " : " << n_large_ << endl;
}
int main(){
Histogram h (0,10,10); // 间隔为10的10个区间
for (int k = 0; k != 10000; ++k){
h.record((int)(10.0*rand() / rand()));
}
h.output_to(cout);
return 0;
}
#include "stdafx.h"
#include <iostream>
#include<typeinfo>
#include<stdexcept>
#include<string>
using namespace std;
//向量方式
class Vec_queue{
// unsigned == unsigned int。两者一样
public:
inline Vec_queue(unsigned capacity = default_capacity);
~Vec_queue(){ delete[] queue_; }
bool empty(){ return head_ == tail_; }
inline char dequeue();
inline void enqueue(char);
bool full() const{ return head_ == (tail_ + 1) %capacity_; }
static bool const fixed_capacity = true;
private:
static unsigned const default_capacity = 32; //默认大小
char * queue_; //数组指针
unsigned head_, tail_; //头尾
unsigned const capacity_; //空间大小
};
inline Vec_queue::Vec_queue(unsigned c)
:queue_(new char[c+1]),head_(0),tail_(0),capacity_(c+1){
}
inline char Vec_queue::dequeue(){
if (!empty()){
char c = queue_[head_];
head_ = (head_ + 1) % capacity_;
return c;
}
else
{
throw underflow_error(string("queue empty!"));
}
}
inline void Vec_queue::enqueue(char c){
if (!full()){
queue_[tail_] = c;
tail_ = (tail_ + 1) % capacity_;
}
else
{
throw overflow_error(string("queue overflow!"));
}
}
//链表方式
class Char_queue{
public:
Char_queue(unsigned capacity = 0) :in_(&out_cell_){
out_cell_.next_ = 0;
}
~Char_queue();
bool empty() const{ return in_==&out_cell_; } //尾指针等于第一个节点
char dequeue();
void enqueue(char);
bool full() const{ return false; } //链表设置为永远不满
static bool const fixed_capacity = false;
private:
struct Cell //一个单元包括值和指向下一个节点的指针
{
Cell * next_;
char c_;
};
Cell out_cell_;//头节点
Cell * in_; //尾指针
};
Char_queue::~Char_queue(){//只保留头结点
for (Cell *p = out_cell_.next_; p;){
Cell * victim = p;
p = p->next_;
delete victim;
}
}
char Char_queue::dequeue(){
if (!empty()){
Cell * victim = out_cell_.next_;
if (victim == in_){//头节点下一个节点是尾节点
in_ = &out_cell_;//尾指针指向头结点
}
char c = victim->c_;
out_cell_.next_ = victim->next_;
delete victim;
return c;
}
else
throw underflow_error(string("queue empty!!!"));
}
void Char_queue::enqueue(char c){
in_->next_ = new(nothrow)Cell;
if (!in_->next_)
throw overflow_error(string("queue 分配空间失败! "));
in_ = in_->next_;
in_->next_ = 0;
in_->c_ = c;
}
int main(){
Char_queue q(4);
while (1){
if (q.empty()){
cout << "queue is empty!" << endl;
}
else if (q.full()){
cout << "queue is full !!" << endl;
}
char cmd, ch;
cout << "请输入命令: e(E):入列;d(D):出列;q(Q):退出;" << endl;
cin >> cmd;
try{
switch (cmd)
{
case 'e':case 'E':cout << "输入入列的值:"; cin >> ch; q.enqueue(ch); break;
case 'd': case 'D': cout << "Dequeued " << q.dequeue() << endl; break;
case 'q': case 'Q': cout << "Quiting !" << endl; return 0 ;
default:
cerr << "Invalid command!" << endl;
break;
}
}
catch(exception &e){
cerr << " Caught exception: " << typeid(e).name() << " ( " << e.what() << " )" << endl;
}
}
}
【相关文档】
1.c++中的#include<new>
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
}
struct GlobalBracket {
GlobalBracket() { std::cout << "Initialize\n"; }
~GlobalBracket() { std::cout << "Clean up\n"; }
} global_bracket_variable;
#include "stdafx.h"
#include <iostream>
#include<iterator>
#include<algorithm>
#include<map>
#include<sstream> //流类型转换
#include<vector>
#include<string>
using namespace std;
typedef map<string, vector<int> > LineIndex;
typedef vector<string>::const_iterator EntryIter;
LineIndex* make_line_index(istream & in, vector<string> const & entries){
LineIndex * index = new LineIndex;
for (EntryIter it = entries.begin(); it != entries.end(); ++it){
(*index)[*it];//建立键
}
string line, word;
int line_number = 0;
cout << "输入每行字符串:(按Ctrl+Z结束)" << endl;
while (getline(in,line))
{
++line_number;
stringstream words(line);
while (words>>word)
{
LineIndex::iterator p = index->find(word);//查找键
if (p != index->end())
(*p).second.push_back(line_number);//添加行号
}
}
return index;
}
int main(int argc ,char * argv[]){
vector<string> entries;
//预设字符串
cout << "预存字符串有:" << endl;
for (int k = 1; k != argc; ++k){
entries.push_back(string(argv[k]));
cout << argv[k] << " ";
}
cout << endl;
LineIndex * index = make_line_index(cin, entries);
vector<string>::iterator p = entries.begin();
for (; p != entries.end(); ++p){
cout << "word: " << *p << " appears in lines: ";
LineIndex::iterator lines = index->find(*p);
//将单词的行号迭代输出
copy((*lines).second.begin(), (*lines).second.end(),
ostream_iterator<int>(cout, " "));
cout << "."<<endl;
}
}
【相关文档】
1.C++ map的基本操作和使用
2.c++ 字符串流 sstream(常用于格式转换)
3.简单的程序诠释C++ STL算法系列之十三:copy
4.STL中istream_iterator和ostream_iterator的基本用法