最近玩Arduino发现,在编写函数库的时候要用到C++。正好手头有一本教材,于是时隔2年,开始重学。

又看到重载、构造、拷贝这些词竟然还有些小兴奋。

开个系列日志记录一下学习过程中的问题和体会。

看到输入输出的时候想到了C++对C的兼容性,于是试了一下printf函数。

 # include<iostream>
# include<stdio.h>
using namespace std; int main()
{
cout<<"C++ iostream"<<endl;
printf("C standard io"); return ;
}

果然还是不错的。

可是又在StackOverFlow上发现了别人的问题。有人在尝试用printf输出string变量。

 # include <iostream>

 int main()
{
using namespace std; string myString = "Press ENTER to quit program!";
cout << "Come up and C++ me some time." << endl;
printf("Follow this command: %s", myString);
cin.get(); return ;
}

提问者说“Each time the program runs, myString prints a seemingly random string of 3 characters...”

对这个问题,直接引用下面chris大神的回答。

It's compiling because printf isn't type safe, since it uses variable arguments in the C sense. printf has no option for std::string, only a C-style string. Using something else in place of what it expects definitely won't give you the results you want. It's actually undefined behaviour, so anything at all could happen.

The easiest way to fix this, since you're using C++, is printing it normally with std::cout, since std::string supports that through operator overloading:

std::cout << "Follow this command: " << myString;

If, for some reason, you need to extract the C-style string, you can use the c_str() method of std::string to get a const char * that is null-terminated. Using your example:

 #include <iostream>
#include <string> int main()
{
using namespace std; string myString = "Press ENTER to quit program!";
cout << "Come up and C++ me some time." << endl;
printf("Follow this command: %s", myString.c_str()); //note the use of c_str
cin.get(); return ;
}

If you want a function that is like printf, but type safe, look into variadic templates (C++11, supported on all major compilers as of MSVC12). You can find an example of one here. There's nothing I know of implemented like that in the standard library, but there might be in Boost, specifically boost::format.

复制以上代码,在geany下编译的时候没有成功,提示error: ‘printf’ was not declared in this scope,果然加上#include <stdio.h>后编译成功。

由此可见,geany毕竟是一个轻量级的编程环境,main函数必须返回int之类的要求不知道是否是严谨性的体现。总之学习过程中用geany足矣,也希望能借此养成良好的编程习惯。

C++的学习记录 - 0的更多相关文章

  1. HTML5 学习记录——0

    2015/08/19 HTML5的标签功能划分:基础.格式.表单.框架.图像.音视频.链接.列表.表格.样式.元信息.编程 1.HTML基础标题 <h1> - <h6>段落 & ...

  2. Apache Shiro 学习记录4

    今天看了教程的第三章...是关于授权的......和以前一样.....自己也研究了下....我觉得看那篇教程怎么说呢.....总体上是为数不多的精品教程了吧....但是有些地方确实是讲的太少了.... ...

  3. Spring学习记录(九)---通过工厂方法配置bean

    1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...

  4. [Django]模型学习记录篇--基础

    模型学习记录篇,仅仅自己学习时做的记录!!! 实现模型变更的三个步骤: 修改你的模型(在models.py文件中). 运行python manage.py makemigrations ,为这些修改创 ...

  5. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  6. Python学习记录day5

    title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...

  7. Matlab 进阶学习记录

    最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录: 1. conf_proposal  =  proposal_config('image_means', ...

  8. meta的学习记录

    meta的学习记录 最近再学响应式的布局,就先来总结一下我们的viewport. 什么是viewport呢? 手机浏览器是把页面放在一个虚拟的“窗口”(viewport)中,通常这个虚拟的“窗口”(v ...

  9. [ZHUAN]Flask学习记录之Flask-SQLAlchemy

    From: http://www.cnblogs.com/agmcs/p/4445583.html 各种查询方式:http://www.360doc.com/content/12/0608/11/93 ...

随机推荐

  1. Jade学习笔记

    初学nodejs,折腾过用handlebars做模板,后来隔了一段重新学习,用了jade,真心简洁……记录一些学习笔记,以备复习. jade是基于缩进的,所以tab与space不能混用: 属性的设置: ...

  2. flask开发restful api系列(2)

    继续上一章所讲,上一章我们最后面说道,虽然这个是很小的程序,但还有好几个要优化的地方.先复制一下老的view.py代码. # coding:utf-8 from flask import Flask, ...

  3. C语言初学 数学中带根号的复杂计算问题

    #include<stdio.h> #include<math.h> int main() { double a,b; scanf("%lf%lf",&am ...

  4. iOS开发之多媒体API(1)

    iOS开发之多媒体API(1)       播放视频 视频文件介绍 视频格式可以分为适合本地播放的本地影像视频和适合在网络中播放的网络流媒体影像视频两大类.尽管后者在播放的稳定性和播放画面质量上可能没 ...

  5. Swift 总结使用问号(?)和感叹号(!)-备用

    在使用可选类型和可选链时,多次使用了问号(?)和感叹号(!),但是它们的含义是不同的,下面我来详细说明一下. 1. 可选类型中的问号(?) 声明这个类型是可选类型,访问这种类型的变量或常量时要使用感叹 ...

  6. Linux下关闭node应用

    今天在折腾用node接入微信公众号时,碰到了node应用启动后卡死退出,需要找出该进程关闭的问题,由于对shell脚本不是很熟悉,记录如下: 我们在用npm start启动应用后,通常要关闭时,ctr ...

  7. android应用开发之Window,View和WindowManager .

    ViewManager  vm = a.getWindowManager(); vm.add(view,l); window :一个抽象的窗口基类,控制顶层窗口的外观和行为.作为顶层窗口,可控制窗口背 ...

  8. Android Fragment getActivity返回null解决

    在Android开发中,如果我们用到V4包里面的Fragment,在应用被切换到后台的时候,Activity可能被回收,但是创建的所有Fragment则会被保存到Bundle里面,下面是Fragmen ...

  9. vim 中Ctags的安装和使用

    Ctags是一个用来为源文件中的标识符(如变量.函数.类成员.宏定义等)创建索引文件的程序.这些tags文件能被编辑器或其它工具用来快速查找定位源代码中的符号(tag/symbol),如变量名,函数名 ...

  10. java.lang.ClassNotFoundException错误原因汇总

    开发java很长时间了,还经常会遇到java.lang.ClassNotFoundException这样的错误,最近又处理了一次,起初怀疑是jdk版本比class文件的编译版本低了导致了,但是运维人员 ...