cout

C++ SegFault when dereferencing a pointer for cout

99封情书 提交于 2019-11-28 01:10:32
I'm new to C++ and just trying to get a hang of it. It generally seems not too bad, but I stumbled upon this weird/pathological segfaulting behavior: int main () { int* b; *b = 27; int c = *b; cout << "c points to " << c << endl; //OK printf( "b points to %d\n", *b); //OK // cout << "b points to " << (*b) << endl; - Not OK: segfaults! return 0; } This program, as given, produces what you'd expect: c points to 27 b points to 27 On the other hand, if you uncomment the second-to-last line, you get a program that crashes (seg-fault) in runtime. Why? This is a valid pointer. int* b points to an

Check if ostream object is cout or ofstream, c++

☆樱花仙子☆ 提交于 2019-11-28 00:03:26
Is there a way in C++ to check if an ostream object is cout or a ofstream object? Something like: ostream& output(ostream& out) { if (out == cout) return out; else { out << "something different because its not going to the console" << endl; return out; } } The reason I want to do this, is that I want to overload the << operator to do two different things depending on what type of stream it is used with. Is it possible to just overload the << operator twice each time with a different type of stream? Updated to reflect intention better. It's possible by checking the stream's 'identity': if (

C++ not showing cout in Xcode console but runs perfectly in Terminal

偶尔善良 提交于 2019-11-27 23:23:50
Basically i am running a simple program in Xcode Version 8.3 (8E162) #include <iostream> using namespace std; int main() { int a; cout << "What is your age: "; cin >> a; cout << "My age is " << a << endl; return 0; } I have seen different questions about cout need to be flushed and all std::cout won't print and Xcode debugger not showing C++ cout output . The Xcode debugger does not print cout until i put \n or endl . But, it works perfectly fine on terminal. What if i had to use What is your age: and the user input age in the single line rather than the next line putting \n and endl ? This is

#leetCode刷题纪实 Day17

北战南征 提交于 2019-11-27 23:10:27
https://leetcode-cn.com/problems/walking-robot-simulation/ 机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向前移动 x 个单位长度 在网格上有一些格子被视为障碍物。 第 i 个障碍物位于网格点 (obstacles[i][0], obstacles[i][1]) 如果机器人试图走到障碍物上方,那么它将停留在障碍物的前一个网格方块上,但仍然可以继续该路线的其余部分。 返回从原点到机器人的最大欧式距离的平方。 示例 1: 输入: commands = [4,-1,3], obstacles = [] 输出: 25 解释: 机器人将会到达 (3, 4) 示例 2: 输入: commands = [4,-1,4,-2,4], obstacles = [[2,4]] 输出: 65 解释: 机器人在左转走到 (1, 8) 之前将被困在 (1, 4) 处 提示: 0 <= commands.length <= 10000 0 <= obstacles.length <= 10000 -30000 <= obstacle[i][0] <= 30000 -30000 <= obstacle[i][1] <

多线程中的detach

為{幸葍}努か 提交于 2019-11-27 19:19:51
从 thread 对象分离执行的线程,允许执行独立地持续。一旦线程退出,则释放所有分配的资源。(就是两个线程彼此相互独立) 调用 detach 后, *this 不再占有任何线程。 #include <iostream> #include <chrono> #include <thread> void independentThread() { std::cout << "Starting concurrent thread.\n"; std::this_thread::sleep_for(std::chrono::seconds(2)); std::cout << "Exiting concurrent thread.\n"; } void threadCaller() { std::cout << "Starting thread caller.\n"; std::thread t(independentThread); t.detach(); //分离,主线程和子线程彼此独立进行:不会出现join()的那种等待子线程结束再执行主线程;也不会出现什么都不操作,主线程结束后杀掉子线程,报abort的错误. //可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached,不然会报abort错误. std::this

C++优先队列详解

北战南征 提交于 2019-11-27 19:04:35
转自csdn的文章,仅作为学习笔记。原文链接: https://blog.csdn.net/weixin_36888577/article/details/79937886 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。 在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出 (first in, largest out)的行为特征。 首先要包含头文件 #include<queue> , 他和 queue 不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队。 优先队列具有队列的所有特性,包括队列的基本操作,只是在这基础上添加了内部的一个排序,它本质是一个堆实现的。 和队列基本操作相同: 1.top 访问队头元素 2.empty 队列是否为空 3.size 返回队列内元素个数 4.push 插入元素到队尾 (并排序) 5.emplace 原地构造一个元素并插入队列 6.pop 弹出队头元素 7.swap 交换内容 定义:priority_queue<Type, Container, Functional> Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list

Output aligned columns

前提是你 提交于 2019-11-27 18:34:55
问题 I am learning C++. I have a problem formatting the output of my program. I would like to print there columns perfectly aligned but so far I cannot do it, here is my code: int main() { employee employees[5]; employees[0].setEmployee("Stone", 35.75, 053); employees[1].setEmployee("Rubble", 12, 163); employees[2].setEmployee("Flintstone", 15.75, 97); employees[3].setEmployee("Pebble", 10.25, 104); employees[4].setEmployee("Rockwall", 22.75, 15); printEmployees(employees, 5); return 0; } // print

Why is std::cout not printing the correct value for my int8_t number?

不羁的心 提交于 2019-11-27 17:49:27
问题 I have something like: int8_t value; value = -27; std::cout << value << std::endl; When I run my program I get a wrong random value of <E5> outputted to the screen, but when I run the program in gdb and use p value it prints out -27, which is the correct value. Does anyone have any ideas? 回答1: Because int8_t is the same as signed char , and char is not treated as a number by the stream. Cast into e.g. int16_t std::cout << static_cast<int16_t>(value) << std::endl; and you'll get the correct

c++的构造和析构

蹲街弑〆低调 提交于 2019-11-27 17:42:53
//文件名ss.h 1 #pragma once 2 class ss 3 { 4 private: 5 char*p;//利用指针来为p申请对内存 6 float height; 7 int age=5; 8 char sex; 9 public: 10 ss(); 11 void eat(); 12 ~ss(); 13 }; //文件名ss.cpp 1 #include "ss.h" 2 #include<iostream> 3 using std::cout; 4 using std::endl;//利用命名空间的第三种方式来调用头文件里命名空间std中的方法 5 ss::ss() 6 { 7 p = new char[5]; 8 cout << "构造函数" << endl; 9 } 10 11 12 ss::~ss() 13 { 14 if (p != nullptr){ 15 delete[] p; 16 p = nullptr; 17 cout << "析构函数"<<endl; 18 } 19 } 1 #include<iostream> 2 #include"ss.h" 3 using namespace std; 4 5 int main(){ 6 { ss a; 7 a.eat();//调用类ss中a方法中的eat 8 }//将析构函数显示出来利用{

undefined reference to 'std::cout'

穿精又带淫゛_ 提交于 2019-11-27 16:54:49
Shall this be the example: #include <iostream> using namespace std; int main() { cout << "Hola, moondo.\n"; } It throws the error: gcc -c main.cpp gcc -o edit main.o main.o: In function `main': main.cpp:(.text+0xa): undefined reference to `std::cout' main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)' main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init