cout

C++: 'cout << pointer << ++pointer' generates a compiler warning

倾然丶 夕夏残阳落幕 提交于 2019-12-01 16:56:06
I have a C++ learning demo here: char c = 'M'; short s = 10; long l = 1002; char * cptr = &c; short * sptr = &s; long * lptr = &l; cout << "cptr:\t" << static_cast<void*>(cptr) << '\n'; cout << "cptr++:\t" << static_cast<void*>(++cptr) << '\n'; cout << "sptr:\t" << sptr << '\n'; cout << "sptr++:\t" << ++sptr << '\n'; cout << "lptr:\t" << lptr << '\n'; cout << "lptr++:\t" << ++lptr << '\n'; cout << c << '\t' << static_cast<void*>(cptr) << '\t' << static_cast<void*>(++cptr) << '\n'; cout << s << '\t' << sptr << '\t' << ++sptr << '\n'; cout<< l << '\t' << lptr << '\t'<< ++lptr << '\n'; The

Why is writing a std::string to cout causing an unknown operator << error?

时光怂恿深爱的人放手 提交于 2019-12-01 16:37:45
I am getting an error when I try to output the return value from one of my methods: Error: No operator "<<" matches these operands. Operand types are: std::ostream << std::string Main.cpp #include <iostream> using namespace std; #include "Book.h" int main() { book.setTitle("Advanced C++ Programming"); book.setAuthorName("Linda", "Smith"); book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond"); book.setPrice(49.99); cout << book.getBookInfo(); // <-= this won't compile because of the error above. int i; cin >> i; return 0; }; Method which should return string: string Book:

C++操作Mysql数据库/Linux下

廉价感情. 提交于 2019-12-01 16:26:00
本文链接:https://blog.csdn.net/Tanswer_/article/details/72796570 想用C++写项目,数据库是必须的,所以这两天学了一下C++操作Mysql数据库的方法。也没有什么教程,就是在网上搜的知识,下面汇总一下。 连接MySQL数据库有两种方法:第一种是使用ADO连接,不过这种只适合Windows平台;第二种是使用MySQL自己的C API函数连接数据库。我是在Linux平台下开发,所以就采用第二种方法,有很多Api函数,但是常用的就几个,我也是就用到其中的几个。 API函数 1.mysql_real_connect() 连接一个mysql服务器 MYSQL *mysql_real_connect (MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) 如果连接成功,返回MYSQL*连接句柄。如果连接失败,返回NULL。对于成功的连接,返回值与第1个参数的值相同 2.mysql_query() 执行指定”以NULL终结的字符串”的SQL查询 返回一个结果表,假定查询成功,可以调用

C++: How can i create a function that accepts concatenated strings as parameter?

旧街凉风 提交于 2019-12-01 16:22:34
问题 Can i design my logging-function in a way, that it accepts concatenated strings of the following form using C++? int i = 1; customLoggFunction("My Integer i = " << i << "."); . customLoggFunction( [...] ){ [...] std::cout << "Debug Message: " << myLoggMessage << std::endl << std::endl } Edit: Using std::string as the attribute to the function works for the concatenated string, but then a passed non-concatenated string like customLoggFunction("example string") produces a compile-time error

C++03:封装->继承->多态

巧了我就是萌 提交于 2019-12-01 15:59:15
一.简介 二.使用继承 三.继承的调用顺序 #pragma once #include <iostream> using namespace std; class F{ public: F(){ k5 = 10; cout << "F:F()" << endl; } ~F(){ cout << "F:~F()" << endl; } //virtual ~F(){} void FuncA(){ cout << "F:FuncA()" << endl; } virtual void FuncB() { cout << "F::FuncB()" << endl; } int k5; protected: int k4; }; class Z : public F{ public: Z(){ k5 = 5; cout << "Z:Z()" << endl; } ~Z(){ cout << "Z:~Z()" << endl; } void FuncA(){ cout << "Z::FuncA()" << endl; } void FuncB() { cout << "Z::FuncB()" << endl; } int k5; protected: int k4; }; #include "Jicheng.h" int main() { F* a = new F(); //F() cout

测试代码

亡梦爱人 提交于 2019-12-01 13:47:49
//在POV.cc中 //4发送查询用户结果 ErrorMessage POV::handleUserSendQuery(rapidjson::Document& doc) { ErrorMessage msg; msg.type="UserSend"; if(!doc.HasMember("name")) { msg.errcode=343; msg.msg="json中不包含name字段"; std::cout << "msg.errcode:" << msg.errcode << "\nmsg.msg:" << msg.msg << "\n"; return msg; } std::string name=doc["name"].GetString(); if(!blockchain.hasData("name",name,"user")) { msg.errcode=241; msg.msg="数据库中不存在该用户,查询失败"; std::cout << "msg.errcode:" << msg.errcode << "\nmsg.msg:" << msg.msg << "\n"; return msg; } else { rapidjson::Document& old_data=blockchain.getDataFromDatabase("name",name,

PTA(Basic Level)1024.科学计数法

随声附和 提交于 2019-12-01 13:26:03
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9] . [0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部分的正负号即使对正数也必定明确给出。 现以科学计数法的格式给出实数 A ,请编写程序按普通数字表示法输出 A ,并保证所有有效位都被保留。 输入格式: 每个输入包含 1 个测试用例,即一个以科学计数法表示的实数 A 。该数字的存储长度不超过 9999 字节,且其指数的绝对值不超过 9999。 输出格式: 对每个测试用例,在一行中按普通数字表示法输出 A ,并保证所有有效位都被保留,包括末尾的 0。 输入样例 1: +1.23400E-03 输出样例 1: 0.00123400 输入样例 2: -1.2E+10 输出样例 2: -12000000000 思路 字符串处理的问题,重点是存储好 E 前的数字,还有就是要定好指数的大小 一个容易错的点是指数比较小的时候,小数点是不能忽略的,要计算好关系在适当的时候输出小数点,距离 +1.23E+00 ,此时的输出应该为 1.23 ,如果一律按照指数比较大来处理,会有一个测试点 TLE 的 代码 #include<bits/stdc++.h> using namespace std; char a[10100]; int main() {

priority_queue member function

时间秒杀一切 提交于 2019-12-01 13:24:40
没有优先队列的dijkstra不算真的dijkstra 所以我又回来补常识了 <1>priority_queue::emplace <7>priority_queue::top 1 // priority_queue::emplace 2 #include <iostream> // std::cout 3 #include <queue> // std::priority_queue 4 #include <string> // std::string 5 6 int main () 7 { 8 std::priority_queue<std::string> mypq; 9 10 mypq.emplace("orange"); 11 mypq.emplace("strawberry"); 12 mypq.emplace("apple"); 13 mypq.emplace("pear"); 14 15 std::cout << "mypq contains:"; 16 while (!mypq.empty()) 17 { 18 std::cout << ' ' << mypq.top(); 19 mypq.pop(); 20 } 21 std::cout << '\n'; 22 23 return 0; 24 } 25 //Ooutput:mypq contains:

Get precision of cout

瘦欲@ 提交于 2019-12-01 10:52:21
I can set precision of cout output using cout.precision(precision_value); how can I get the precision value, which is used by cout already? I can't find any other related function in cout description . The related problem I faced is how can I change cout precision for some part of a code only (so it need to be reset afterwards to exact previous value). I tryed: //save original cout flags std::ios_base::fmtflags coutFlags = cout.flags(); cout.precision(1); cout.setf(ios::fixed); // code cout.flags(coutFlags); but this doens't work. jrok The function is overloaded . Both overloads return the

表达式不能自加减?

試著忘記壹切 提交于 2019-12-01 10:33:41
如下程序段 int x = 3, y = 4, z = 5; cout << &(++x); cout << x+++++y; cout << &(x++); cout << ++x++; cout << ++x+++y+++z << endl; (假设每条cout语句独立运行)求输出结果,思考一下吧 . . . ... ... 好了,看结果 第一个cout输出地址0x6dfee4 第二个cout报错 "lvalue required as increment operand" 第三个cout报错 "lvalue required as '&' operand" 第四个cout报错 "lvalue required as increment operand" 第五个cout报错 "lvalue required as increment operand" 这里从一连串的自增运算符开始说起。 首先 , 自增运算符的操作数operand必须是能够存储数据的变量,而不是常量数 这就牵扯到前缀自增与后缀自增的区别了 前缀自增是先自增,再将变量代入表达式计算 后缀自增是先将值代入表达式计算,再自增(注意这里说的是将值代入,而不是将变量代入) 后缀 自增/自减运算符优先级 高于 前缀 自增/自减的一块空间。 因此,我们对这四个程序一一分析。 第一个表达式是&(++x),先执行++x