[C++] 用Xcode来写C++程序[2] 操作变量
用Xcode来写C++程序[2] 操作变量

此节讲解包括变量的初始化的几种方式,以及泛型编程的两种变量赋值方式.
最基本的变量赋值以及操作:
// operating with variables #include <iostream>
using namespace std; int main ()
{
// 声明变量
int a, b;
int result; // 赋值
a = ;
b = ;
a = a + ;
result = a - b; // 打印结果
cout << result; return ;
}
带有构造器的初始化方式:
// initialization of variables #include <iostream>
using namespace std; int main ()
{
int a = ; // 普通初始化
int b(); // constructor initialization 构造器初始化
int c{}; // uniform initialization 联合初始化 (2011的C++版本中被提出来)
int result; // 定义没有赋值的变量 a = a + b;
result = a - c;
cout << result << endl; return ;
}
类型推演赋值:
// initialization of variables #include <iostream>
using namespace std; int main ()
{
/**
* 类型推演 (会降低程序可读性)
*
* auto
* decltype
*
*/ // auto
int foo = ;
auto bar = foo; // bar与foo类型一致,并且赋了值,其值为7
cout << bar << endl; // decltype
decltype(foo) cat; // cat与foo类型一致,不过没有赋值
cout << cat << endl; return ;
}
打印:
7
0
Program ended with exit code: 0
操作字符串:(注意,需要引入头文件string)
// my first string
#include <iostream>
#include <string> using namespace std; int main ()
{
// 定义字符串
string mystring; // 字符串赋值
mystring = "This is a string"; // 输出字符串
cout << mystring << endl; return ;
}
当然,你也可以用以下的几种方式初始化字符串:
string mystring = "This is a string"; // 常规模式
string mystring ("This is a string"); // 构造器模式
string mystring {"This is a string"}; // 联合初始化模式
[C++] 用Xcode来写C++程序[2] 操作变量的更多相关文章
- [C++] 用Xcode来写C++程序[7] Class
用Xcode来写C++程序[7] Class 不带构造函数的Rectangle类 // // Rectangle.h // Plus // // Created by YouXianMing on 1 ...
- [C++] 用Xcode来写C++程序[6] Name visibility
用Xcode来写C++程序[6] Name visibility 此小结包括了命名空间的一些使用细节 命名空间 #include <iostream> using namespace st ...
- [C++] 用Xcode来写C++程序[5] 函数的重载与模板
用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespa ...
- [C++] 用Xcode来写C++程序[4] 函数
用Xcode来写C++程序[4] 函数 此节包括引用函数,内联函数,防止修改函数入参,函数自身带有默认值. 引用函数:防止复制对象,减少系统开销 内联函数:编译的时候根据具体情形将代码嵌入进去,成不成 ...
- [C++] 用Xcode来写C++程序[3] Constants
用Xcode来写C++程序[3] Constants 以下是一些基本数据的含义: 75 // int 75u // unsigned int 75l // long 75ul // unsigned ...
- [C++] 用Xcode来写C++程序[1] 新建C++项目工程
用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created ...
- 使用Xcode IDE写node.js
最近在玩node.js 但是发现很多IDE就是用不顺手 后来发现Xcode可以剖析java script 于是试着使用Xcode来当做node.js的编辑器 首先,在Mac上必须先安装node.js的 ...
- 使用Code::blocks在windows下写网络程序
使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...
- JAVA-集合作业-已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数
第二题 已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...
随机推荐
- Ethereum 源码分析之 accounts
一.Account // Account represents an Ethereum account located at a specific location defined // by the ...
- 17-hadoop-yarn安装
在搭建好的 resourceHA的环境下, 配置yarn yarn是 hadoop2以后的计算框架, 通过ResourceManager的调用, 将我们写的程序包分到各个nodeManager上, 由 ...
- Linux-(which,whereis,locate,find)
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置 ...
- mysql查看权限的命令
mysql查看用户权限的命令 1.这里用来查看用户存储过程: show grants for 用户; eg: show grants for root@'localhost';#这样就会把root用户 ...
- 【起】ACM类下为过往所做过的部分算法题目
[起]ACM类下为过往所做过的部分算法题目 几百道题,日后细细品味.
- [IMX6]Android6.0移植和分析
0. 知识点 中断(设备树) [IMX6]设备树分析--dts 1. 编译 Android6.0内核移植(1):分析编译日志 Android6.0内核移植(2):kernel编译内核 单独烧录kern ...
- dev中ASPxListBox单选和多选的设置
只需要设置SelectionMode,为Multiple时是单选,CheckColumn时是多选
- why go
取自:http://www.weixinyidu.com/n_3502636 Go的核心贡献者 Go主要有静态语言.天生并发.内置GC.安全性高.语法简单.交叉编译和编译快速这几个方面的特性.这些特性 ...
- js中var与let
问题 for (var iii = 0; iii < 3; iii++) { setTimeout(function(){ console.debug(iii) }, 1000) let let ...
- java 获取两个日期之间的所有日期(年月日)
前言:直接上代码 java 获取两个日期之间的所有日期(年月日) /** * 获取两个日期之间的日期,包括开始结束日期 * @param start 开始日期 * @param end 结束日期 * ...