cout

STL迭代器

牧云@^-^@ 提交于 2020-01-07 04:22:33
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 以下内容大多来自《C++标准程序库》 迭代器是一个“可遍历STL容器内全部或部分元素”的对象,一个迭代器用来指出容器中的一个特定位置。 基本操作: (1)Operator * :返回当前位置上的元素值 (2)Operator ++:将迭代器前进至下一个元素,大多数迭代器还可以使用operator -- 退回到前一个元素 (3)Operator ==和Operator != 判断两个迭代器是否指向同一个位置 (4)Operator = 为迭代器赋值 这些操作与操作数组元素时的指针接口是一致的,不同在于迭代器具有遍历复杂数据结构的能力,其下层运行机制取决于其所遍历的数据结构,因此,每一种容器型别都必须提供自己的迭代器。事实上每一种容器都将其迭代器以嵌套方式定义于内部,因此各种迭代器的接口相同,型别却不相同。 重要函数 所有容器类别都提供有一些成员函数,使我们得以获得迭代器并一直遍访所有元素。 begin() 返回一个迭代器,指向容器起始点的位置 end() 返回一个迭代器,指向容器结束点。结束点在最后一个元素之后,这样的迭代器又称作逾尾(past-the-end)迭代器(所以遍历的时候都是判断!=end()) #include <iostream> #include <list> using namespace

打印二叉树的边界节点

自闭症网瘾萝莉.ら 提交于 2020-01-06 21:33:06
问题描述 从根节点起,逆时针输出二叉树所有的边界节点: 根节点 每层最左侧节点 每层最右侧节点 底层的所有叶子节点,比如14、15等。 上述的结果是: 1 2 4 7 11 13 14 15 16 12 10 6 3 空间限制是 O ( h ) O(h) O ( h ) ,时间限制是 O ( N ) O(N) O ( N ) , h h h 是树的高度, N N N 是节点的个数。 思路和代码 需要的节点分为三种: 左边界:一层中最左侧的节点,1, 2, 4, 7, 11, 13;为了方便,根节点算左边界 右边界:一层中最右侧的节点,16, 12, 10, 6, 3 底层叶子节点:叶子节点,而且不属于左右边界,14, 15 根据节点的特性,在 前序遍历 的时候, 同一层中 最先访问到的节点是左边界节点;最后访问到的是右边界节点。根据这个特性,可以计算左右边界。 如果一个节点没有左右孩子,而且不是左右边界,则该节点一定是底层叶子节点。 问题的难点在于,怎样在 O ( h ) O(h) O ( h ) 的空间和 O ( N ) O(N) O ( N ) 的时间中计算左右边界;因为底层叶子节点求解,在叶子节点中排除左右边界即可。 我们建立一个数组 edgeNodes[H][2] , H 是树的高度。 edgeNodes[h][0] 表示这层的左边界, edgeNodes[h][0]

c++_day1/2_cin cout

五迷三道 提交于 2020-01-06 19:11:09
编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行的任何位置。当用户按下enter键,数据输入停止。程序自动对所有的整数进行求和并打印出结果。 需要解决两个问题,提取数字,提取连续数字。 //c #include<stdio.h> void main() { int sum=0;; int i; char ch; while(scanf("%d",&i)==1) { sum+=i; while((ch=getchar())==' ')//屏蔽空格 ; if(ch=='\n') break; ungetc(ch,stdin);//将变量ch中存放的字符退回给stdin输入流。 } printf("sum; %d\n",sum); }    //c++ #include<iostream> using namespace std; int main() { int sum=0; cout<<"请输入"; int i; while(cin>>i) { sum+=i; while(cin.peek()==' ') { cin.get(); } if(cin.peek()=='\n') break; } cout<<"sum:"<<sum<<endl; return 0; } ①表达式cin>>i返回输入流对象本身也就是cin

睡眠数毫秒

戏子无情 提交于 2020-01-06 19:11:01
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我知道POSIX sleep(x) 函数使程序休眠x秒。 在C ++中是否有使程序休眠x 毫秒 的功能? #1楼 在C ++ 11中,可以使用标准库工具来执行此操作: #include <chrono> #include <thread> std::this_thread::sleep_for(std::chrono::milliseconds(x)); 清晰易读,无需再猜测 sleep() 函数采用的单位。 #2楼 #include <windows.h> 句法: Sleep ( __in DWORD dwMilliseconds ); 用法: Sleep (1000); //Sleeps for 1000 ms or 1 sec #3楼 为什么不使用time.h库? 在Windows和POSIX系统上运行: #include <iostream> #include <time.h> using namespace std; void sleepcp(int milliseconds); void sleepcp(int milliseconds) // Cross-platform sleep function { clock_t time_end; time_end = clock() +

STL——算法

懵懂的女人 提交于 2020-01-06 15:30:33
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 以下内容大多摘自《C++标准程序库》 算法实例 STL提供了一些标准算法,包括搜寻、排序、拷贝、重新排序、修改、数值运算等。算法并不是容器类别的成员函数,而是一种搭配迭代器使用的全局函数。 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> coll; vector<int>::iterator pos; //coll.push_back(2); coll.push_back(5); coll.push_back(4); coll.push_back(1); coll.push_back(6); coll.push_back(3); pos = min_element(coll.begin(),coll.end()); //寻找最小值 cout << "min:" << *pos << endl; pos = max_element(coll.begin(), coll.end());//寻找最大值 cout << "max:" << *pos << endl; sort(coll.begin(),coll.end()); //排序 cout <<

C++ vector和unordered_map浅析

有些话、适合烂在心里 提交于 2020-01-05 05:28:29
C++内置的数组支持容器的机制,可是它不支持容器抽象的语义。要解决此问题我们自己实现这种类。在标准C++中,用容器向量(vector)实现。容器向量也是一个类模板。可以说vector的引入,方便了我们对一系列数据的处理,相比于数组,我们不用考虑下标越界等溢出问题了。 使用vecor,需要引入头文件#include <vector>,命名空间需要引入using std::vector,再补充一下,vector是连续存储的!!! 简单的存储操作: vector<int> v1; for (vector<int>::size_type i = 0; i < 10; i++) { v1.push_back(i); } cout << "adjective:"; for (vector<int>::size_type i = 0; i < 10; i++) { cout << v1[i] << setw(4); } 然后比较坑的这个,注意一下就OK了: //v1中有n个值为i的元素 vector<int> v1(n,i); //v2中只有n和i两个元素 vector<int> v2({n,i}); 切记我们初始化的时候不能直接v1[i]=value,只能使用v1.push_back(i); 详细的介绍请看 https://www.cnblogs.com/mengfanrong/p

Output a C++ string including all escape characters

馋奶兔 提交于 2020-01-04 05:25:15
问题 I have a string like this: string s = "\t Hello \n"; When I print it then it gives me a tab then Hello then a new line. However, is there anyway I can print it such that I see this in my console: \t Hello \n In other words, I want the string to disregard the escape characters and treat it as an actual string? 回答1: You would need to escape the backslash so it would look something like this string s = "\\t Hello \\n"; 回答2: Put doublebackslash because \\ doublebackslash produce a \ single

Passing variable to jsp

北慕城南 提交于 2020-01-04 02:59:33
问题 I have a java class public void doView( RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { //I need to pass the string variable over to my jsp ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY); String sLang_Id = themeDisplay.getLanguageId(); include("/html/mypackage/view.jsp", renderRequest, renderResponse); } How would I read sLang_Id in my jsp <c:out value="${sLang_Id}" /> ??? 回答1: Add the following

Passing variable to jsp

早过忘川 提交于 2020-01-04 02:59:07
问题 I have a java class public void doView( RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { //I need to pass the string variable over to my jsp ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY); String sLang_Id = themeDisplay.getLanguageId(); include("/html/mypackage/view.jsp", renderRequest, renderResponse); } How would I read sLang_Id in my jsp <c:out value="${sLang_Id}" /> ??? 回答1: Add the following

Cout and Cin in Linux - can't see the console

℡╲_俬逩灬. 提交于 2020-01-03 18:52:15
问题 I just moved from Windows to Linux and I'm try to create a simple application that opens a console, displays a message and wait for key press to close. I have created it on Windows and it works, then I just moved the files to Linux. Didn't make any change, just compiled it with g++ and I get no errors. The problem is that on Linux (Ubuntu 12.04) I can't see the console and some message asking me to press any key before closing. My code is as simple as this: #include <iostream> #include