[C++] 用Xcode来写C++程序[2] 操作变量
2023-09-14 08:57:16 时间
用Xcode来写C++程序[2] 操作变量
此节讲解包括变量的初始化的几种方式,以及泛型编程的两种变量赋值方式.
最基本的变量赋值以及操作:
// operating with variables #include iostream using namespace std; int main () // 声明变量 int a, b; int result; // 赋值 a = 5; b = 2; a = a + 1; result = a - b; // 打印结果 cout result; return 0; }
带有构造器的初始化方式:
// initialization of variables #include iostream using namespace std; int main () int a = 5; // 普通初始化 int b(3); // constructor initialization 构造器初始化 int c{2}; // uniform initialization 联合初始化 (2011的C++版本中被提出来) int result; // 定义没有赋值的变量 a = a + b; result = a - c; cout result endl; return 0; }
类型推演赋值:
// initialization of variables #include iostream using namespace std; int main () * 类型推演 (会降低程序可读性) * auto * decltype // auto int foo = 7; auto bar = foo; // bar与foo类型一致,并且赋了值,其值为7 cout bar endl; // decltype decltype(foo) cat; // cat与foo类型一致,不过没有赋值 cout cat endl; return 0; }
打印:
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 0; }
当然,你也可以用以下的几种方式初始化字符串:
string mystring = "This is a string"; // 常规模式 string mystring ("This is a string"); // 构造器模式 string mystring {"This is a string"}; // 联合初始化模式
Xcode中修改变量名、类名及字符串的替换操作 在做iOS开发代码优化的工作时,优化代码结构之前,我们应该先整理好工程的外貌,将文件和类的命名进行规范,在Xcode中为我们提供了方便而强大的名称修改功能。
Xcode与C++之游戏开发:Pong游戏 Xcode与C++之游戏开发:Pong游戏 接下来在前两天游戏骨架的基础上实现一个经典的乒乓球(Pong)游戏。游戏是这样的,一个球在屏幕上移动,玩家控制球拍来击打球。可以说乒乓球游戏是游戏开发者的 “Hello World” 项目。
相关文章
- [C++] 用Xcode来写C++程序[7] Class
- [C++] 用Xcode来写C++程序[6] Name visibility
- C/C++ 头文件以及库的搜索路径
- EasyDSS RTMP流媒体服务器的HTTP接口query url的C++实现方法
- Atitit.java jna 调用c++ dll 的总结
- [C++ 面试基础知识总结]字符串,向量和数组
- VS中c++文件调用c 函数 ,fatal error C1853 预编译头文件来自编译器的早期版本号,或者预编译头为 C++ 而在 C 中使用它(或相反)
- 【面试攻略】C++面试高级/资深篇
- Xcode如何创建C++项目
- c++ vector 初始化_C++--vector()的用法
- C++链表冒泡,归并,插入排序(纯指针)
- c++ vector C++ vector存放结构体 并且排序
- C++函数调用操作符重载()
- C++代码中的边界效应引起的问题
- PAT 1149 C++ 版
- 第十三届蓝桥杯C++C组省赛H题,Python A 组省赛 F 题,Ptyhon C 组省赛 H 题—— 重新排序(AC)
- C++使用技巧(二十):再学public、protected及private用法