资料人员来学C++ #随堂笔记 Day1

前情提要: 本人从事数据处理的工作大约四年之久,主要的语言为R、SQL and Python,身为数据程序人这几年这麽竞争,不时还是会上线上课程,近来的课程刚好有C++的需求,注册一门课程直接上下去。
以下是课程的笔记,本人非程序本科出身主要会使用我写别程序语言的逻辑来说明。
一、基本格式
1.include就像是 R 里面的library 、python 里面的 import ,iostream就是套件了。
2.有一个要注意的 带入套件的时候 可以使用 <套件> 和 "套件",<>会直接去抓标准套件,而""会先抓你本地端的套件。
3.int main 就是主要的写法去执行程序,里面就是输入要执行的内容,cout就c out 的意思打印出後面的文字"Hello world, I am ready for C++"。

 #include <iostream>

 int main() 
{
     std::cout << "Hello world, I am ready for C++";
     return 0;
 }

二、文字注解
总共有两种
1.单行 // 只能一行
2.多行 /* 好几行
第二行*/

三、using namespace
主要就是在指定一些功能的时候要选择要使用的模块类似 std::cout 中 前面的 std,但是如果你前面加了using namespace std; 代表之後的程序码只要打count,前面不需要再加std::。
老师解说他有时会这样使用有时候不会,有时候你的project太大可能很复杂就比较不会用using namespace 的方式,因地制宜。

四、变数
1.const 不能修改
前面如果加了const不能後面再写入其他的值。
const int weightGoal = 100;
weightGoal = 200;
2.输出
可以使用文字加上变数输出,A为变数。
PS \n 换行
\t 按下一次tab键一样功能
cout<<"int size = "<< A <<"\n";

五、IO(输入、输出)
使用 fstream library

这里他只有稍微简单介绍一下,大概就是ifstream(in)和ofstream(out)的两种功能。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  //create an output stream to write to the file
  //append the new lines to the end of the file
  ofstream myfileI ("input.txt", ios::app);
  if (myfileI.is_open())
  {
      myfileI << "\nI am adding a line.\n";
      myfileI << "I am adding another line.\n";
      myfileI.close();
  }
  else cout << "Unable to open file for writing";

  //create an input stream to read the file
  ifstream myfileO ("input.txt");
  //During the creation of ifstream, the file is opened. 
  //So we do not have explicitly open the file. 
  if (myfileO.is_open())
  {
      while ( getline (myfileO,line) )
      {
          cout << line << '\n';
      }
      myfileO.close();
  }

  else cout << "Unable to open file for reading";

  return 0;
}

六、Header Files
把一些设定放在Header Files里面,再把他include进来,
例如 #include ""main.hpp"

七、cin和input.txt
可以使用cin和input.txt来作为参数的输入
PS 其中cin 中间不能有空白,有空白就要使用getline。

/*This program accepts inputs from the input.txt file*/ 

#include <iostream>
#include <string>


int main()
{
   std::string userName; 
   std::cout<<"Tell me your nickname?: ";
   std::getline(std::cin, userName);
   std::cout<<"Hello "<<userName<<"\n";
   return 0;
}

八、资料格式转换
使用sstream,然後用stringstream转成inches
#include
std::getline (std::cin,stringLength);
std::stringstream(stringLength) >> inches;
std::cout<<"You entered "<<inches<<"\n";

九、最後介绍了资料格式

main.cpp - the main program
main.hpp - the header file
mainFunctions.cpp - a file for any functions

input.txt - for user inputs. Delete the first line of the file,
otherwise the line will be seen as an input.


<<:  RPA应用技术交流║给UiPath开发者的线上大会 2021

>>:  前端工程日记 30日 名片设计

JS Library 学习笔记:首先当然来试试 jQuery (三)

除了监听事件外,jQuery也提供了定义好的动态效果函式,让开发者直接使用,并透过传入相关参数,去自...

2.4.6 Design System - Carousel

学习曲线这件事 有时候,起步的阶段最累最难 Carousel 轮播器其实也是各种专案常遇到的元件 ...

Day 26 - 新鲜人带新鲜人篇

早上七点半的闹钟,大概是进入职场刚开始比较不习惯的事情(这应该是要报到的前端新鲜人的心情),今天的内...

企业资料通讯Week5 (2) | electronic mail [SMTP部分]

electronic mail 三要件 1. user agents(UA) 邮件使用者代理人,也叫...

2020it邦铁人赛-30天手把手的Vue.js教学 Day29 - 关心时事! 做个简单的COVID-19追踪app吧!(中)

tags: Vue.js ItIron2020 前言 昨天我们完成了专案的基本建置并建立一个简单的c...