C++-前缀和后缀
1,c++规定后缀形式的++操作符有一个int行的参数,被调用时,编译器自动加一个0作为参数给他
2,前缀返回一个reference,后缀返回一个const对象
///////////////////////////////////////////////////////////////////////////////
//
// FileName : meffect_item5.h
// Version : 0.10
// Author : Ryan Han
// Date : 2013/11/18
// Comment : prefix and postfix
//
///////////////////////////////////////////////////////////////////////////////
#ifndef MEFFECT_ITEM5_H
#define MEFFECT_ITEM5_H
#include <iostream>
using namespace std; class A{
friend ostream& operator <<(ostream &os, const A &a);
public:
A(int initvalue=, int initstep=):value(initvalue), step(initstep){} //prefix
A& operator++(){
cout << "A& operator++() was called" << endl;
value+=step;
return *this;
} //postfix
const A operator++(int){
cout << "const A operator++(int) was called." << endl;
const A oldA = *this;
++(*this);
return oldA;
}
private:
int value;
int step;
}; #endif
///////////////////////////////////////////////////////////////////////////////
//
// FileName : meffect_item5.cpp
// Version : 0.10
// Author : Ryan Han
// Date : 2013/11/18
// Comment : prefix and postfix
//
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "meffect_item5.h"
using namespace std; ostream& operator<<(ostream &os, const A&a)
{
return os<<a.value;
}
int main(){
int i = ; //postfix return a const value
//i++++; //prefix return a reference
++++i; A a(,);
cout << a++ << endl;
cout << ++a << endl;
A b(,);
cout << b.operator++() << endl;
cout << b.operator++() << endl; return ;
}
C++-前缀和后缀的更多相关文章
- 递归算法(二)——前缀转后缀
源码:pretopost.cpp #include "stdafx.h" #include <stdio.h> #include <stack> /**** ...
- POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)
给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...
- 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树
另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...
- 关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理
一.删除前缀 '*' #include<iostream> #include<cstdio> using namespace std; //主函数 int main() { ] ...
- POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)
题目链接:http://poj.org/problem?id=2752 题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度 思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后 ...
- 【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G
问题 G: JS Window 时间限制: 2 Sec 内存限制: 512 MB 题目描述 JSZKC has an array A of N integers. More over, he has ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- HDU6025 Coprime Sequence —— 前缀和 & 后缀和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025 Coprime Sequence Time Limit: 2000/1000 MS (Java/ ...
- bootstrap历练实例:复选框或单选按钮作为输入框组的前缀或后缀
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- bootstrap历练实例:按钮作为输入框组前缀或后缀
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
随机推荐
- UIButton(在代码中使用)
- (void)viewDidLoad { [super viewDidLoad]; // 1.1 创建按钮对象 // UIButton *button = [[UIButton alloc] ini ...
- uva 11324 The Largest Clique
vjudge 上题目链接:uva 11324 scc + dp,根据大白书上的思路:" 同一个强连通分量中的点要么都选,要么不选.把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它 ...
- Objective-C与C++的区别
1.两者的最大相同:都是从C演化而来的面相对象语言,两者都兼容标准C语言 2.两者的最大不同:Objective-C提供了运行期动态绑定机制,而C++是编译静态绑定,并且通过嵌入类(多重继承)和虚函数 ...
- Integer封装与拆箱
Integer封装与拆箱 简介: 目录: Integer自动封装的陷阱 Integer自动拆箱机制 Integer自动封装的陷阱 public class IntegerDemo { public s ...
- stdlib标准库的常用API
iOS 有如下四种随机数方法,下面以产生 [0,100) 的随机数为例: 1. srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % ...
- commonJS — 事件处理(for Event)
for Event github: https://github.com/laixiangran/commonJS/blob/master/src/forEvent.js 代码 (function(w ...
- 栈 - 从零开始实现by C++
参考链接:数据结构探险-栈篇 学了队列之后,栈就很简单了,换汤不换药. 栈 栈的模型 后进先出(电梯,进制转换,括号的匹配检测) 栈的基本元素 栈顶,栈底(一般很少用到),栈容量,栈长度 注意 ...
- js高级程序设计(三)基本概念
数据类型 ECMAscript中有五种简单数据类型Undefined,Null,Boolean,Number,String 还有一种复杂数据类型Object. typeof操作符 typeof可能返回 ...
- JavaWeb学习之环境搭建
1. HTML(Hyper Text Markup Language) , 超文本标记语言. HTML文件的后缀名一般是: .htm , .html 表单(form): 浏览器内核: WebKit , ...
- jquery用Ajax中的回调函数时注意事项
前端代码 <script language="javascript" type="text/javascript" src="<?php ...