cout

STL : 反向迭代器(Reverse Iterator)

女生的网名这么多〃 提交于 2019-12-27 04:04:32
1. 定义 反向迭代器(Reverse Iterator)是一种反向遍历容器的迭代器。也就是,从最后一个元素到第一个元素遍历容器。反向迭代器将自增(和自减)的含义反过来了:对于反向迭代器,++运算将访问前一个元素,而--运算则访问下一个元素。 2. 作用 (1)反向迭代器需要使用自减操作符:标准容器上的迭代器(reverse_iterator)既支持自增运算,也支持自减运算。但是,流迭代器由于不能反向遍历流,因此流迭代器不能创建反向迭代器。 (2)可以通过reverse_iterator::base()将反向迭代器转换为普通迭代器使用,从逆序得到普通次序。这是因为:有些容器的成员函数只接受iterator类型的参数,所以如果你想要在ri所指的位置插入一个新元素,你不能直接这么做,因为vector的insert函数不接受reverse_iterator。如果你想要删除ri 所指位置上的元素也会有同样的问题。erase成员函数会拒绝reverse_iterator,坚持要求iterator。为了完成删除和一些形式的插入操作,你必须先通过base函数将reverse_iterator转换成iterator,然后用iterator来完成工作。 3. 例子 1 void test_reverse() 2 { 3 int a[] = {-2, -1, 0, 1, 2, 3, 4}; 4 std

C++ Primer Plus解读(第2章(共18章))

一个人想着一个人 提交于 2019-12-26 10:16:14
一、创建C++程序与C++程序的一般格式 1.C++的基本结构 int main() { statements return 0 ; } 其中: ①函数头对函数与程序其他部分之间的接口进行了总结,函数体指出函数应该做什么的计算机指令; ②C++中,每一条完整的指令都称之为语句,所有的语句都以分号结束(分号也叫终止符,是语句结束的标记,不可省略)。 ③返回语句:结束该函数。 ④调用函数与被调用函数:返回类型描述被调用函数返回给调用函数信息的属性;形参列表描述调用函数传递给被调用函数的信息。 二、#include 编译指令 预处理器:处理以#开头的编译指令,典型预处理器操作:在源代码被编译之前,替换或添加文本(例如:#include 的作用是在最终编译之前,使用iostream文件内容替换该编译指令)。 C++头文件没有扩展名(这是一个动态发展的过程)。是指在include的时候不加后缀。由此产生一种名称空间的说法。 名称空间旨在解决大型程序以及多个商家的程序组合更容易。可通过名称空间调用不同的同名函数。 最初我们都使用iostream.h与cout的组合,后来增加了名称空间的特性后,大家都不愿改之前的代码,即使用iostream并添加std,便于使用搞出了一条using namespace std的指令(潜在的问题是如果std中与其他部分有重名函数或者变量

栈解决迷宫问题

馋奶兔 提交于 2019-12-26 06:57:37
题目描述 迷宫是一个二维矩阵,其中1为墙,0为路,3为入口,4为出口.要求从入口开始,从出口结束,按照 下,左,上,右 的顺序来搜索路径. 输入 迷宫宽度w 迷宫高度h 迷宫第一行 迷宫第二行 ... 迷宫第h 行 输出 入口横坐标1 入口纵坐标1 横坐标2 纵坐标2 横坐标3 纵坐标3 横坐标4 纵坐标4 ... 横坐标n-1 纵坐标n-1 出口横坐标n 出口纵坐标n 样例输入 8 10 1 1 1 1 1 1 1 1 1 0 1 1 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 3 1 0 1 1 1 0 0 1 0 0 4 1 1 0 0 0 0 1 1 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 样例输出 3 3 2 3 2 4 2 5 3 5 3 6 3 7 4 7 4 6 4 5 4 4 5 4 6 4 代码如下: #include <iostream> #include <cstring> #include <stack> #include <cstdio> using namespace std; int w, h; const int maxn = 1000; int maze[maxn][maxn]; struct node { int x,y;//1,2,3

c++ stl deque容器

不想你离开。 提交于 2019-12-25 15:30:01
c++中,Deque容器和vector相似,deque内部也采用动态数组来管理元素,支持随机存取。。头文件<deque> 1.deque和vector的不同之处: 1)两端都可以较快速地按插元素和删除元素,而vector只能在尾端进行 2)在对内存有所限制的系统中,deque可以包含更多的元素,因为它不止一块内存。因此deque的max_size()可能更大 3)deque不支持对容量和内存的重分配时机的控制。 4)deque的内存区块不再被使用时,会被释放。deque的内存大小是可缩减的,由实作版本定义。 2.和vecotr差不多的特性: 1)在中端部分按插、移除元素的速度相对较慢。 2)迭代器属于random access iterator(随机存取迭代器) 3.deque的常用函数: 1)构造函数 deque<T> c deque<T> c(n) 产生一个deque,含有n个元素,这些元素均以default构造函数产生 deque<T> c(n,elem) 产生一个含有n个值都是elem的deque deque<T> c(c2) c2中的所有元素都被拷贝来初始化c deque<T> c(beg,end) 以[beg,end]内的元素为初值 2)非变动性操作: c.size()   实际元素个数 c.empty()   判空 c.max_size() 

Can't write the last part of a txt file to cout using ifstream

半世苍凉 提交于 2019-12-25 02:31:30
问题 The code below will print all of the text from the sample text file I'm using except for the last little snippet of it. I think this has something to do with the eof or the byte size I'm using not working as I expect. #include <iostream> #include <fstream> using namespace std; int main(int argc, char* argv[]){ int length; char* buffer; //get file stream and open local file. ifstream stream; stream.open("SampleFile.txt", ios::binary); stream.seekg(0, ios::end); length = stream.tellg(); stream

报错:cout不明确

半世苍凉 提交于 2019-12-25 00:18:26
问题: 报错:cout不明确 解决办法: 删除 using namespace std; 保存 加上 using namespace std; 保存 来源: CSDN 作者: csuzhucong 链接: https://blog.csdn.net/nameofcsdn/article/details/103689836

print 64bit c++ full memory address

旧街凉风 提交于 2019-12-24 23:37:45
问题 While I was writing code on a 64 bit machine for a c++ program,I noticed that printing the address of a variable (for example) returns just 12 hexadecimal characters, instead of 16. Here's an example code: int a = 3 ; cout sizeof(&a) << " bytes" << endl ; cout << &a << endl ; The output is: 8 bytes 0x7fff007bcce0 Obviously, the address of a variable is 8 byte (64 bit system). But when I print it, I get only 12 hexadecimal digits instead of 16. Why this? I think that is due to the fact that

C++ cout and enum representations

那年仲夏 提交于 2019-12-24 01:51:10
问题 I have an enum that I'm doing a cout on, as in: cout << myenum . When I debug, I can see the value as an integer value but when cout spits it out, it shows up as the text representation. Any idea what cout is doing behind the scenes? I need that same type of functionality, and there are examples out there converting enum values to string but that seems like we need to know what those values are ahead of time. In my case, I don't. I need to take any ol' enum and get its text representation. In

Strange std::cout behaviour with const char*

大憨熊 提交于 2019-12-23 18:55:45
问题 I have a method which returns a string to display as an error message. Depending on where this error occurs in the program, I might add a bit more of an explanation to the error message before displaying it. string errorMessage() { return "this is an error"; } // somewhere in the program... const char* message = ("Some extra info \n" + errorMessage()).c_str(); cout << message << endl; (I am storing the message as a const char* since I will actually provide this error to another method which

Xcode not showing anything in console with C++

↘锁芯ラ 提交于 2019-12-23 17:40:59
问题 I was just trying to use Xcode for a very small C++ project, and wanted to see some prints in the console, the thing is i did not see anything. I tried to run a very simple code instead: #include <iostream> int main (int argc, char * const argv[]) { std::cout << "Hello, World!\n"; printf("here"); return 0; } but still, nothing in Xcode console. any idea why? EDIT: adding snapshot of the program: EDIT 2: found this, and its working: How do I run a C++ program in XCode 4? 回答1: Your image doesn