cout

类的大小2

给你一囗甜甜゛ 提交于 2019-12-02 06:31:06
#include<stdio.h> #include<stdlib.h> #include<iostream> #include<iomanip> using namespace std; class A { public: int m_data1;//12 int m_data2;//16 void func1() {} void func2() {} virtual void vfunc1(){}//8 virtual void vfunc2(){}//8 }; class B : public A { public: int m_data3;//20 + 4(对齐) void func2() {} virtual void vfunc1(){} //16 }; class C : public B { public: int m_data1; int m_data4; void func2() {} virtual void vfunc1(){} }; class D { int a; }; int main() { cout << "sizeof(D):" << sizeof(D) << endl; cout << "sizeof(A):" << sizeof(A) << endl; cout << "sizeof(B):" << sizeof(B) << endl;

链栈的实现

谁都会走 提交于 2019-12-02 06:16:06
 链栈的实现 栈可谓是一种简单的数据结构,在 C++ 有库函数可以调用 #include<stack> 我们也可以亲手写一个链栈。链栈的实现形式是通过链表来储存,使用单向链表,使用头插法,头的位置称之为栈顶,其尾部的位置成为栈底。 栈的操作有 进栈 , 出栈 , 判空 , 访问栈顶 等操作。自己写栈的结构可以添加自己需要的操作 图示: main.c #include"Stack_my.h" using namespace std; int main() { Stack_my<int>s1; s1.Push(1); cout <<"Stack top: "<< s1.Top() << endl; s1.Push(2); cout << "Stack top: " << s1.Top() << endl; if (s1.Empty()) cout << "Empty." << endl; else cout << "Not Empty." << endl; s1.Pop(); cout << "Stack top: " << s1.Top() << endl; s1.Pop(); cout << "Stack top: " << s1.Top() << endl; if (s1.Empty()) cout << "Empty." << endl; else cout << "Not

Redirecting in C++

不问归期 提交于 2019-12-02 05:54:16
#include <iostream> #include <fstream> using namespace std; void foo(){ streambuf *psbuf; ofstream filestr; filestr.open ("test.txt"); psbuf = filestr.rdbuf(); cout.rdbuf(psbuf); } int main () { foo(); cout << "This is written to the file"; return 0; } Does cout write to the given file? If not, is there a way to do it without sending the variables to foo, like new ? update : I can't use a solution that uses class or uses global so plz can some give me solution that use new. Also passing the from main to foo streambuf *psbuf; ofstream filestr; should work right? I am trying to do this but its

C++ vector 容器使用

依然范特西╮ 提交于 2019-12-02 04:55:22
C++ vector 容器使用 vector 作用在此不做解释了 使用需要引入头文件 #include <vector> 自带函数如下 1. push_back 在容器的最后添加一个数据 2. pop_back 去掉容器的最后一个数据 3. at 得到下标位置的数据 4. begin 得到容器第一个数据的指针 5. end 得到容器最后一个数据下一个的指针 6. front 得到容器第一个数据的引用 7. back 得到容器最后一个数据的引用 8. max_size 得到容器最大可以存放数据个数 9. capacity 当前vector分配的空间可以存储数据个数 10. size 当前存储的数据个数 11. resize 改变容器可以存储数据个数 12. reserve 改变容器分配空间的大小 13. erase 删除指针指向的数据项 14. clear 清空当前的vector 15. rbegin 返回容器数据反转后的开始指针(原来的 end - 1 ) 16. rend 返回容器反转后结束指针(原来的begin - 1 ) 17. empty 判断容器是否为空 18. swap 与另一个容器交换数据 // vector 运用实例如下 // Pro1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <vector>

C++ Vector容器

亡梦爱人 提交于 2019-12-02 04:54:43
//Vector 是一种类模板,能够存储类型相同的对象,每个对象有个与之对应的索引。 //模板:可以看作是编译器生成类或函数编写的一份说明,编译器根据模板创建或函数的过程称为实例化,模板需要指出其类型。模板<类型> 模板变量; //模板不能生成为引用,因为引用不是对象,Vector可以嵌套使用。 //C++11之前的容器定义为:vector<vector<int> >,现在为vector<vector<int>> //容器初始化:vector<T> v1 "一个空vector" // vector<T> v2(v1) / vector<T> v2 = v1 / vector<T> v2 = v1 / vector<T> v3(n,val) / vector<T> v4(n) / vector<T> v5{a,b,a...} / vector<T> v5 = {a,b,c,...} // 对于vector<T> v4(n) 省略初始值,库会根据vector中元素的类型给容器中的所有元素赋一个初始值(但有些类必须要进行初始化) //通过花括号和圆括号区分初始的是元素的数量还是元素值 //如果提供的是花括号,但是其中的值不能用来列表初始化,那么就应该是用来构造vector对象。eg:vector<string> vec{10};//10个默认的初始化元素 //vector 添加元素

Chapter03 第一节 简单变量

混江龙づ霸主 提交于 2019-12-02 04:54:17
3.1 简单变量 定义一个变量后,系统根据变量类型的不同在内存的不同区域分配一个空间,将值复制到内存中,然后用户通过变量名访问这个空间。 3.1.1 变量名 变量名的命名规则: 只能使用字母、数字、下划线 第一个字符不能是数字 区分大小写 不能使用关键字 以两个下划线/下划线和大学字符开头的名称保留给实现使用,以一个下划线开头的名称保留给实现,用作全局标识符(c++程序的所有源文件可用) 部分环境对变量名长度有要求 3.1.2 & 3.1.3 整型以及其扩展 计算机的基本单位是bit(位),一个bit分为0/1两种状态,8bit能表示256种不同的状态。一个字节通常意义上由8bit组成。1KB=1024字节。 int、short、long、long long 都表示整型,但是他们的位数不同,所以表示的数值的范围也就不同。不同的类型规定了不同的最小长度。具体如下: Short 16位 int 16位 long 32位 long long 64位 cppreference详细说明 不同位数的系统中,整型的长度不一样。主要区别在于long类型和指针类型。16位机器中,long占4字节,不支持longlong类型,指针为2个字节。32位机器中,long占4字节,longlong占8个字节,指针占4个字节。64位系统中,long占8个字节,longlong8个字节,指针8个字节。 //

Strange behaviour of std::cout in Linux

孤人 提交于 2019-12-02 04:52:32
I am trying to print results in 2 nested for cycles using std::cout . However, the results are not printed to the console immediately, but with delay (after both for cycles or the program have been finished). I do not consider such behavior normal, under Windows printing works OK. The program does not use threads. Where could be the problem? (Ubuntu 10.10 + NetBeans 6.9). std::cout is an stream, and it is buffered. You can flush it by several ways: std::cout.flush(); std::cout << std::flush; std::cout << std::endl; // same as: std::cout << "\n" << std::flush` johny: I am flushing the buffer

Why I am getting garbage(unwanted) output here?

狂风中的少年 提交于 2019-12-02 04:40:19
Whenever I am writing this following code, I am getting garbage(unexpected) output in some online compiler, but if I use code block then getting satisfied output. So my question is why I am getting this type of output? for example, if I input 5 7 + 5 - 10 - 20 + 40 - 20 then I am getting 22 1 in the code block. But in the online compiler, it's something else. #include<iostream> #include<cstdlib> using namespace std; int main() { int have, n, i; int kid=0; cin>>n>>have; int line[n]; for(i=0;i<n;i++) { cin>>line[i]; if(line[i]>=0) have+=line[i]; else { if(have>=abs(line[i])) have+=line[i]; else

std::cout not working inside a for-loop

丶灬走出姿态 提交于 2019-12-02 03:39:43
问题 I'm new to C++, and right now I'm learning from the book called Accelerated C++ . I finished the third chapter (vectors), and I came to this exercise: "Write a program to count how many times each distinct word appears in its input." After some thinking, I started working on it. I wanted to test the program, but std::cout wasn't working. I put cout << "test"; on a few places in my code to see where's the problem, and the conclusion is that it doesn't work inside the first for-loop. Don't

ERROR: no match for 'operator<<" in 'std::cout

大憨熊 提交于 2019-12-02 03:22:37
问题 I realize this error is usually due to some syntax or type issues but I am not sure how to solve this problem. I think it may do with the type of findRt. vector<triangle> findRightTriangles(unsigned long l, unsigned long h) { <triangle> retval; // storage for return value. triangle t; double what; for(t.s1 = 3; t.s1 <= h; t.s1++) { for(t.s2 = t.s1; t.s2 <= h; t.s2++) { what = sqrt((t.s1*t.s1) + (t.s2*t.s2)); t.s3 = static_cast<unsigned int>(what); if(((t.s1*t.s1)+(t.s2*t.s2)) != (t.s3*t.s3) |