cout

采用邻接矩阵表示图的深度优先搜索遍历(与深度优先搜索遍历连通图的递归算法仅仅是DFS的遍历方式变了)

若如初见. 提交于 2019-12-04 18:32:36
//采用邻接矩阵表示图的深度优先搜索遍历(与深度优先搜索遍历连通图的递归算法仅仅是DFS的遍历方式变了) #include <iostream> using namespace std; #define MVNun 100 typedef char VerTexType; typedef int ArcType; typedef struct { VerTexType vexs[MVNun]; ArcType arcs[MVNun][MVNun]; int vexnum, arcnum; }Graph; bool visited[MVNun]; int FirstAdjVex(Graph G, int v); int NextAdjVex(Graph G, int v, int w); int LocateVex(Graph G, VerTexType v) { for (int i = 0;i < G.vexnum;++i) { if (G.vexs[i] == v) return i; } return -1; } void CreateUDN(Graph& G) { int i, j, k; cout << "请输入总顶点数,总边数 , 以空格隔开:"; cin >> G.vexnum >> G.arcnum; cout << endl; cout << "输入点的名称,如

深度优先搜索遍历非连通图

倖福魔咒の 提交于 2019-12-04 18:32:34
//深度优先搜索遍历非连通图 #include <iostream> using namespace std; #define MVNum 100 typedef char VerTexType; typedef int ArcType; typedef struct { VerTexType vexs[MVNum]; ArcType arcs[MVNum][MVNum]; int vexnum, arcnum; }Graph; bool visited[MVNum]; int FirstAdjVex(Graph G, int v); int NextAdjVex(Graph G, int v, int w); int LocateVex(Graph G, VerTexType v) { for (int i = 0;i < G.vexnum;++i) { if (G.vexs[i] == v) return i; } return -1; } void CreateUDN(Graph &G) { int i, j, k; cout << "请输入总顶点数,总边数,以空格隔开:"; cin >> G.vexnum >> G.arcnum; cout << endl; cout << "输入点的名称,如a" << endl; for (i = 0;i < G.vexnum;++i)

采用邻接表表示图的深度优先搜索遍历

ⅰ亾dé卋堺 提交于 2019-12-04 18:32:27
//采用邻接表表示图的深度优先搜索遍历 #include <iostream> using namespace std; #define MVNum 100 typedef char VerTexType; typedef char VerTexType; typedef struct ArcNode { int adjvex; struct ArcNode* nextarc; }ArcNode; typedef struct VNode { VerTexType data; ArcNode* firstarc; }VNode, AdjList[MVNum]; typedef struct { AdjList vertices; int vexnum, arcnum; }ALGraph; bool visited[MVNum]; int LocateVex(ALGraph G, VerTexType v) { for (int i = 0;i < G.vexnum;++i) if (G.vertices[i].data == v) return i; return -1; } void CreateUDG(ALGraph& G) { int i, k; cout << "请输入总顶点数,总边数,以空格隔开:"; cin >> G.vexnum >> G.arcnum; cout <

No match for operator << in std::cout [duplicate]

淺唱寂寞╮ 提交于 2019-12-04 17:25:10
This question already has answers here : no match for ‘operator<<’ in ‘std::operator (4 answers) Closed 2 years ago . I'm trying to print every element inside a vector like this: vector<users>::iterator i; for(i = userlist.begin(); i<userlist.end(); i++) { cout << *i << "\n"; } Then I'm getting an error like this: no match for 'operator<<' in 'std::cout << (&i)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = users*, _Container = std::vector<users, std::allocator<users> >]()' Is it anything obvious I've missed? Have you defined a function with this signature?:

C++ 虚函数

北战南征 提交于 2019-12-04 16:24:52
父类函数不加virtual关键词,子类继承后,当父类指针指向子类指针,同样的函数,会执行父类的函数。子类的函数实际是被隐藏了,如果用子类的指针指向自己的话,是能够执行的。 #include <iostream> /** * C++多态 虚函数 */ using namespace std; class Shape { public: Shape(); ~Shape(); double calcArea(); }; class Circle : public Shape { public: Circle(double r); ~Circle(); double calcArea(); protected: double m_dR; protected: string m_strName; }; class Rect : public Shape { public: Rect(double width, double height); ~Rect(); double calcArea(); protected: double m_dWidth; double m_dHeight; }; Rect::Rect(double width, double height) { m_dHeight = height; m_dWidth = width; cout << "Rect::Rect()

【UOJ388】配对树(dsu on tree+线段树)

泪湿孤枕 提交于 2019-12-04 16:15:23
传送门 题意: 给出一颗含有 \(n\) 个结点的无根树,之后给出一个长度为 \(m\) 的序列,每个元素在 \([1,n]\) 之间。 现在序列中每个长度为偶数的区间的完成时间定义为树上最小配对方法中每对匹配点间距离的总和。 现在要求所有长度为偶数的区间的完成时间的和。 思路: 首先不妨将这颗树转化为有根树,最终不会影响答案。 注意到性质:偶数个点的两两匹配方式是唯一的,都是最深的两个点相互匹配,这样才能保证没有重复计算的边。 在子树内部直接计算不好算,要考虑很多东西(一开始就想偏了QAQ)。因为匹配方式唯一,所以子树中若全部匹配完成,那么至多只会剩下一个点。显然,若剩下一个点,此时子树及其父亲这条边肯定会算上。 我们将问题转化为单独考虑一条边的贡献,一条边的贡献次数即为序列元素在该子树中出现奇数次的偶数区间个数。 考虑暴力计算:对于当前的子树,暴力给序列上打上标记,然后做一个前缀和,若存在 \(i,j,i<j\) ,满足 \(i\equiv j(mod\ 2)\) 且 \(s_j\equiv s_i+1(mod\ 2)\) 即可。 如果用线段树维护,那么只需要维护奇数位置、偶数位置上面前缀模 \(2\) 意义下 \(1\) 的个数即可快速求得当前子树内部的答案。 时间复杂度 \(O(n^2logn)\) 。 因为这涉及到子树问题,且不带修改,所以可以直接施展 \(dsu\ on

C++入门

元气小坏坏 提交于 2019-12-04 16:07:58
目录 一、容器 1.vector数组 2.Set集合 3.map映射 4.stack栈 5.queue队列 二、算法 1.bitset位运算 2.sort排序 三、C11特性 1.auto声明 2.to_string 3.stoi、stod... 一、容器 1.vector数组 主要内容: ①定义方式 ②输出单个元素 ③遍历输出 主要函数: ①获取大小v.size() ②获取首指针,获取尾指针v.begin()/v.end() #include <iostream> #include <vector> using namespace std; int main(void){ //定义方式 vector<int> v1; vector<int> v2(10); vector<int> v3(10,4); //输出单个元素 cout << v2[0] << endl;//默认为0 cout << v3[0] << endl;//输出4 //获取大小 cout << v2.size() << endl; cout << v3.size() << endl; //遍历方式 for(int i = 0; i < v3.size(); i++){ cout << v3[i] << " "; } cout << endl; //常用遍历方式:迭代器 for(auto it = v3.begin(

std::cin really slow

旧巷老猫 提交于 2019-12-04 15:13:57
问题 So I was trying to write myself a command for a linux pipeline. Think of it as a replica of gnu 'cat' or 'sed', that takes input from stdin, does some processing and writes to stdout. I originally wrote an AWK script but wanted more performance so I used the following c++ code: std::string crtLine; crtLine.reserve(1000); while (true) { std::getline(std::cin, crtLine); if (!std::cin) // failbit (EOF immediately found) or badbit (I/O error) break; std::cout << crtLine << "\n"; } This is exactly

C++之程序流程_选择结构

◇◆丶佛笑我妖孽 提交于 2019-12-04 14:26:13
C/C++支持最基本的三种程序运行结构:==顺序结构、选择结构、循环结构== * 顺序结构:程序按顺序执行,不发生跳转 * 选择结构:依据条件是否满足,有选择的执行相应功能 * 循环结构:依据条件是否满足,循环多次执行某段代码 选择结构   if语句   **作用:**执行满足条件的语句   if语句的三种形式   * 单行格式if语句   * 多行格式if语句   * 多条件的if语句 单行格式if语句:if (条件) { 条件满足执行的语句 } 1 int main() { 2 3 //选择结构-单行if语句 4 //输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印 5 6 int score = 0; 7 cout << "请输入一个分数:" << endl; 8 cin >> score; 9 10 cout << "您输入的分数为: " << score << endl; 11 12 //if语句 13 //注意事项,在if判断语句后面,不要加分号 14 if (score > 600) 15 { 16 cout << "我考上了一本大学!!!" << endl; 17 } 18 19 system("pause"); 20 21 return 0; 22 } 多行格式if语句:if (条件) { 条件满足执行的语句 } else {

父类子类指针相互转换问题

核能气质少年 提交于 2019-12-04 14:20:36
父类子类指针相互转换问题 1.当自己的类指针指向自己类的对象时,无论调用的是虚函数还是实函数,其调用的都是自己的: 2.当指向父类对象的父类指针被强制转换成子类指针时候,子类指针调用函数时,只有非重写函数是自己的,虚函数是父类的; 3.当指向子类对象的子类指针被强制转换成父类指针的时候,也就是父类指针指向子类对象,此时,父类指针调用的虚函数都是子类的,而非虚函数都是自己的; 将上面三句话总结成一句话就是: 当父类子类有同名非虚函数的时候,调用的是转换后的指针类型的函数;               当父类子类有同名虚函数的时候呢,调用的是指针转换前指向的对象类型的函数。 详见以下代码: 1 #include <iostream> 2 using namespace std; 3 class Base { 4 public: 5 virtual void f() { cout << "Base::f" << endl; } 6 virtual void g() { cout << "Base::g" << endl; } 7 void h() { cout << "Base::h" << endl; } 8 9 }; 10 class Derived:public Base 11 { 12 public: 13 virtual void f(){cout<<"Derived::f"<