cout

How to remove last character put to std::cout?

▼魔方 西西 提交于 2019-12-03 18:40:22
问题 Is it possible on Windows without using WinAPI? 回答1: You may not remove last character. But you can get the similar effect by overwriting the last character. For that, you need to move the console cursor backwards by outputting a '\b' (backspace) character like shown below. #include<iostream> using namespace std; int main() { cout<<"Hi"; cout<<'\b'; //Cursor moves 1 position backwards cout<<" "; //Overwrites letter 'i' with space } So the output would be H 回答2: No. You can't without accessing

C++中int与string的相互转换

匆匆过客 提交于 2019-12-03 16:55:45
一、int转string 1.c++11标准增加了全局函数std::to_string: string to_string (int val); string to_string (long val); string to_string (long long val); string to_string (unsigned val); string to_string (unsigned long val); string to_string (unsigned long long val); string to_string (float val); string to_string (double val); string to_string (long double val); Example: // to_string example #include <iostream> // std::cout #include <string> // std::string, std::to_string int main () { std::string pi = "pi is " + std::to_string(3.1415926); std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number

getting cout output to a std::string

大兔子大兔子 提交于 2019-12-03 14:30:04
问题 I have the following cout statement. I use char arrays because I have to pass to vsnprintf to convert variable argument list and store in Msg . Is there any way we can get cout output to C++ std::string ? char Msg[100]; char appname1[100]; char appname2[100]; char appname3[100]; // I have some logic in function which some string is assigned to Msg. std::cout << Msg << " "<< appname1 <<":"<< appname2 << ":" << appname3 << " " << "!" << getpid() <<" " << "~" << pthread_self() << endl; 回答1: You

深入理解计算机系统 练习题2.25 答案与分析

前提是你 提交于 2019-12-03 13:17:17
#include <stdio.h> #include "stdafx.h" #include <iostream> using namespace std ; float sum_elements( float a[], unsigned length) { int i; float result = 0 ; cout << length << endl; cout << length - 1 << endl; for (i = 0 ; i <= length - 1 ;i++) { result += a[i]; } return result; } int main() { float a[] = { 1.1 , 2 , 2.3 , 3.4 }; cout << sum_elements(a, 0 ) << endl; } 返回结果 可以发现length - 1=4294967295,原因是length的类型是unsigned 但是1的类型是是int,所以计算转为无符号数计算,length - 1=length +(- 1),现在的问题是-1的无符号数是多少-1的十六进制无符号数为0xFFFFFFFF换算之后是4294967295,所以代码就出现了异常,for循环进去之后数组下标越界。 修改为 #include <stdio.h> #include "stdafx.h"

iostream thread safety, must cout and cerr be locked separately?

試著忘記壹切 提交于 2019-12-03 12:09:52
I understand that to avoid output intermixing access to cout and cerr by multiple threads must be synchronized. In a program that uses both cout and cerr, is it sufficient to lock them separately? or is it still unsafe to write to cout and cerr simultaneously? Edit clarification: I understand that cout and cerr are "Thread Safe" in C++11. My question is whether or not a write to cout and a write to cerr by different threads simultaneously can interfere with each other (resulting in interleaved input and such) in the way that two writes to cout can. If you execute this function: void f() { std:

How to print a type vector<pair<char, int>> to screen c++?

纵然是瞬间 提交于 2019-12-03 11:43:33
I have a method that returns a value vector> and I cannot figure out how to print the contents of this vector. I was trying to loop through the contents but I get compiler errors. Here is an example of what I have tried. vector<pair<char, int>> output; for(int i = 0; i < ouput.size; i++) { cout << output[i][i] << endl; //output[i][i] does no work: no operator [] matches these operands } The elements of an std::pair are the first and second data members, so a trivial modification of your loop would print out the contents: for(int i = 0; i < output.size(); i++) { cout << output[i].first << ", "

What is the difference between std::cout and std::wcout?

社会主义新天地 提交于 2019-12-03 10:03:54
问题 In c++ what is the difference between std::cout and std::wcout ? They both control output to a stream buffer or print stuff to the console, or are they just alike ? 回答1: They operate on different character types: std::cout uses char as character type std::wcout uses wchar_t as character type Otherwise both streams write to standard output. 回答2: Another thing is that both are used with respected input stream. Objects of these are initialized during or before the first time an object of std:

How should I correctly assign cout to a static ostream reference variable?

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm defining a class like this: class StaticRuntimeContext { public: enum Verbosity { kHIGH, kMEDIUM, kLOW, kSILENT }; static void Construct(); static std::ostream& stdout1() {return stdout1_;} static std::ostream& stdout2() {return stdout2_;} static std::ostream& stdout3() {return stdout3_;} static std::ostream& stderr() {return stderr_;} protected: private: static std::ostream& stdout1_; static std::ostream& stdout2_; static std::ostream& stdout3_; static std::ostream& stderr_; }; I'm defining the construct function as: void

Opencv标定相机

可紊 提交于 2019-12-03 07:06:22
使用Opencv标定Camera的代码如下: void help(char *argv[]) { } int calib_camera(int argc, char* argv[]) { int n_boards = 0; // Will be set by input list float image_sf = 1.0f;//0.5f; float delay = 1.f; int board_w = 0; int board_h = 0; if (argc < 4 || argc > 6) { cout << "\nERROR: Wrong number of input parameters"; help(argv); return -1; } board_w = atoi(argv[1]); board_h = atoi(argv[2]); n_boards = atoi(argv[3]); if (argc > 4) delay = atof(argv[4]); if (argc > 5) image_sf = atof(argv[5]); int board_n = board_w * board_h; cv::Size board_sz = cv::Size(board_w, board_h); cv::VideoCapture capture(0); if (

C++ STL——异常

偶尔善良 提交于 2019-12-03 07:05:36
目录 一 C++异常机制概述 二 栈解旋(unwinding) 三 异常接口的声明 四 异常类型和异常变量的生命周期 五 C++标准异常库 六 异常的继承 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 C++异常机制概述 什么是异常处理?一句话,异常处理就是处理程序中的错误。 为什么需要异常处理以及异常处理的基本思想? C++之父Bjarne Stroustrup在《The C++ Programming Language》中讲到:一个库的作者可以检测出发生了运行时错误,但一般不知道怎样去处理它们(因为和用户具体的应用有关);另一方面,库的用户知道怎样处理这些错误,但却无法检查它们何时发生(如果能检测,就可以在用户的代码里处理了,不用留给库去发现)。 Bjane Stroustrup说:提供异常的基本目的就是为了处理上面的问题。基本思想是:让一个函数在发现了自己无法处理的错误时抛出(throw)一个异常,然后它的(直接或间接)调用者能够处理这个问题。也就是说,C++将问题的检测与问题的处理相分离。 在异常处理机制出现之前的错误处理方式?在C语言中,对错误的处理总是围绕着两种方法:一是使用整型的返回值标识错误;二是使用errno宏(可以简单理解为一个全局整型变量)去记录错误。当然C++中仍然可以使用这两种方法