cout

stack的简单用法总结

匿名 (未验证) 提交于 2019-12-02 23:06:17
top():返回一个栈顶元素的引用,类型为 T&。如果栈为空,返回值未定义。 push(const T& obj):可以将对象副本压入栈顶。这是通过调用底层容器的 push_back() 函数完成的。 push(T&& obj):以移动对象的方式将对象压入栈顶。这是通过调用底层容器的有右值引用参数的 push_back() 函数完成的。 pop():弹出栈顶元素,直接删除栈顶元素,并没有返回该值哦。 size():返回栈中元素的个数。 empty():在栈中没有元素的情况下返回 true。 emplace():用传入的参数调用构造函数,在栈顶生成对象。 swap(stack & other_stack):将当前栈中的元素和参数中的元素交换。参数所包含元素的类型必须和当前栈的相同。对于 stack 对象有一个特例化的全局函数 swap() 可以使用。 int main(){ //定义一个空栈a stack<int> a; for(int i = 1; i <= 3; i++) a.push(i); cout << "size of a is " << a.size()<< endl; cout << "element of a is: " ; for(; a.size() > 0;){ // cout << a.pop() << " "; pop直接删除元素并没有返回

链表

匿名 (未验证) 提交于 2019-12-02 23:03:14
含有头结点的链表和不含头结点的链表 有和没有的区别在于,我们申请第一个几点时,是否给数据进行初始化。 //含有头节点 Link * head = new Link ; Link * TempLink = head ; for ( int i = 1 ; i < 6 ; ++ i ){ Link * list = new Link ; list -> elem = i ; list -> next = NULL ; TempLink -> next = list ; TempLink = TempList -> next ; } //不含头节点 Link * head = new Link ; head -> elem = 1 ; head -> next = NULL Link * TempLink = head ; for ( int i = 1 ; i < 6 ; ++ i ){ Link * list = new Link ; list -> elem = i ; list -> next = NULL ; TempLink -> next = list ; TempLink = TempList -> next ; } 以及他们在输出时也会有区别,有头结点的需要先TempLink = TempLink->next;在输出TempLink->elem;(先转到储存数据的节点

C++设计缺陷 : cin/cout wcin/wcout

匿名 (未验证) 提交于 2019-12-02 22:56:40
#include<iostream> using std::cin; using std::cout; using std::wcin; using std::wcout; using std::endl; int main() { char *a = new char[100]; wchar_t *b = new wchar_t[100]; cin >> a; wcin >> b; cout << a << endl; wcout << b <<endl; delete[] a; delete[] b; return 0; } #include<iostream> #include<string.h> using namespace std; int main() { ios::sync_with_stdio(false); std::locale::global(std::locale("zh_CN.UTF8")); char *a = new char[100]{'\0'}; wchar_t *b = new wchar_t[100]{L'\0'}; //cin >> a; //cin.clear(); wcin >> b; //cout << a << endl; //cout.clear(); wcout << b <<endl; //wcout.clear(); /

multi_index_container

匿名 (未验证) 提交于 2019-12-02 22:56:40
转自:https://blog.csdn.net/buptman1/article/details/38657807 multi_index_container: Boost Multi-index Containers Library定义了multi_index_container模板类,可以从不同的维度建索引、排序和存取。 如上图,容器multi_index_container分别从shape,number和sequenced(默认插入的顺序)三个维度对元素进行管理。 # include <string> # include <iostream> # include <boost/multi_index_container.hpp> # include <boost/multi_index/member.hpp> # include <boost/multi_index/ordered_index.hpp> using namespace boost ; using namespace boost :: multi_index ; using namespace std ; struct Employee { int id ; string name ; int age ; Employee ( int id_ , std :: string name_ , int age_

protobuf在c++的使用方法以及在linux安装

匿名 (未验证) 提交于 2019-12-02 21:53:52
https://www.cnblogs.com/zhouyang209117/p/7218719.html //SRC_DIR .proto文件存放目录 //--cpp_out 指示编译器生成C++代码,DST_DIR为生成文件存放目录 //XXXX.proto 待编译的协议文件 protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/XXXX.proto    syntax = "proto2"; package tutorial; message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phones = 4; } message AddressBook { repeated Person people = 1; }  把proto文件转化成c++代码.执行下面命令. protoc

C++设计模式:访客模式

懵懂的女人 提交于 2019-12-02 21:15:16
访客模式:通俗的说, 就是定义一个访问者角色, 当对指定角色进行访问时要通过访问者进行访问。 访客模式的侵入性适中,仅在被访问的类里面加一个对外提供接待访问者的接口。 访客模式的优点: 符合单一职责原则. 具体元素角色负责数据的加载, 而访问者角色负责报表的展现, 两个不同的职责非常明确的分离开来, 各自演绎变化. 优秀的扩展. 由于职责分开,继续增加 对数据的操作是非常快捷的. 访客模式应用场景: 对象结构中对象对应的类很少改变,但经常需要在此对象结构上定义新的操作。 需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而需要避免让这些操作"污染"这些对象的类,也不希望在增加新操作时修改这些类。 class Visitor; //被访问类基类: 网站 class Website { public: virtual ~Website(){ std::cout << "~Website()" << std::endl;} virtual void accept(Visitor&) = 0; }; //被访问类具体实现类: 淘宝网 class TaoBao : public Website { public: void accept(Visitor &v) override; void shopping(); }; //被访问类具体实现类: 优酷 class YouKu :

Cout of a string is giving an error and a hard time some insight help pls?

爱⌒轻易说出口 提交于 2019-12-02 18:34:34
问题 I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable.. #include "stdafx.h" #include <iostream> using namespace std; int main() { string name; cout << "Input your name please?" << endl; cin >> name; if { (name == "Bart Simpson") cout << "You have been very naughty" << endl; } return 0; } 回答1: Problems: You have some missing #include s, which probably caused your initial compiler errors. You have a simple syntax

课后编程题(第三章)

心不动则不痛 提交于 2019-12-02 18:13:25
章节总结 编程题预览 题目解答 代码 : #include "stdafx.h" #include<iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { const int convertFactor = 3; cout << "请输入距离___\b\b\b" ; int height = 0; cin >>height ; cout << "您的身高为:" << convertFactor*height << "英尺" << endl; system("pause"); return 0; } 运行结果 : 请输入距离100 您的身高为:300英尺 请按任意键继续. . . 代码: #include "stdafx.h" #include<iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { const int Foot2Inch = 12;//英尺转英寸 const float Inch2Mi = 0.0254;//英寸转米 const float Kg2Pount = 2.2;//千克转磅 int foot = 0; int inch = 0; float pount = 0; cout << "请输入身高

How to redirect cout to console in linux?

匆匆过客 提交于 2019-12-02 17:07:13
问题 I am writing a program which is a part of another program. In the main program, they redirect the default direction of cout to a LOG file. For debugging of my own programm, I need to redirect the output of cout to console (terminal) in linux. I cannot save the console rdbuf like the method described in the example at: http://www.cplusplus.com/reference/iostream/ios/rdbuf/ Is there any way to get the handle to the console of linux in c++ for my purpose? 回答1: You need to define what you mean by

输入输出速度统计

耗尽温柔 提交于 2019-12-02 16:17:55
在学校电脑上跑的文件输入输出。 输出: 测试壹: 输出1e6内数字, 空格 隔开,printf VS cout 1. freopen("1.txt","w",stdout); for(int i=1;i<=1000000;i++){//1e6 printf("%d ",i); } 测试①:2.82 测试②:2.777 测试③:2.847 测试④:2.887 测试⑤:2.797 平均值:2.8256 2. freopen("1.txt","w",stdout); for(int i=1;i<=1000000;i++){ cout<<i<<' '; } 测试①:0.1969 测试②:0.3226 测试③:0.2028 测试④:0.2309 测试⑤:0.1986 平均值:0.23036 小总结:出乎意料……我没关stdout的同步啊,咋快这么多…… 测试贰:printf VS cout ,输出1e6内数字, 回车 隔开 1. freopen("1.txt","w",stdout); for(int i=1;i<=1000000;i++){ printf("%d\n",i); } 测试①:3.073 测试②:2.886 测试③:2.818 测试④:3.145 测试⑤:2.905 平均值:2.9654 小对比:空格和回车对于printf来说变化不大,毕竟只是格式化输出,原理一样。 2