CS106L: Lec02 Types and Structs - Zicx の Blog

CS106L: Lec02 Types and Structs

C++ types and structs

1
2
3
4
5
int val = 5; //32 bits 
char ch = 'F'; //8 bits (usually) 
float decimalVal1 = 5.0; //32 bits (usually) 
double decimalVal2 = 5.0; //64 bits (usually) 
bool bVal = true; //1 bit

String type

1
2
3
4
5
#include <string> // need `string` library
std::string str = "Sarah"; // double quotes means string

// index into a string and get a character
char character = str[1]; // 'a'
注意

C++ is a statically typed language.

statically typed: Everything with a name (variables, functions, etc) is given a type before runtime.(C++)

dynamically typed: Everything with a name (variables, functions, etc) is given a type at runtime based on the thing’s current value.(Python)

Runtime: Period when program is executing commands (after compilation, if compiled).

1
2
3
4
5
# CRASH during runtime, can’t divide a string
def div_3(x):
	return x / 3 

div_3("hello")
1
2
3
4
5
// Compile error: this code will never run
int div_3(int x){
	return x / 3;
}
div_3("hello");

Static typing helps us to prevent errors before our code runs.

Define two functions with the same name but different types.

示例
1
2
3
4
5
6
7
8
9
int half(int x) { 
	std::cout << "1" << endl; 
	return x / 2;
}

double half(double x) {
	cout << "2" << endl;
	return x / 2;
}

struct: A group of named variables each with their own type. A way to bundle different types together.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
struct Student {
	string name; // these are called fields
	string state; // separate these by semicolons
	int age;
};

Student s; // don't have to add `struct` key word
s.name = "Sarah";
s.state = "CA";
s.age = 21; // use . to access fields

// is the same as ...
Student s = {"Sarah", "CA", 21};

// structured binding -- c++17 feature
auto [name, state, age] = s;

std::pair: An STL built-in struct with two fields of any type.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// The fields in std::pairs are named `first` and `second`
// initialize
std::pair<int, string> numSuffix{1, "st"};
std::cout << numSuffix.first << numSuffix.second << std::endl;

// is same as ...
std::pair<int, string> numSuffix = std::make_pair(1, "st");

// also same as ...
std::pair<int, string> numSuffix = {1, "st"};

To avoid specifying the types of a pair, use std::make_pair(field1, field2)

1
2
3
4
5
6
7
std::string suffix;
int num;
std::tie(num, suffix) = std::make_pair(1, "st");
std::cout << num << " " << suffix << std::endl;

// more efficient way in c++17
auto [a, b] = make_pair(3.4, 5);

Use std::tie(arg1, arg2) to accept the pair.

Type deduction with auto.

auto: Keyword used in lieu of type when declaring a variable, tells the compiler to deduce the type.

When to use auto?

  • When you don’t care what the type is (iterators)
  • When its type is clear from context (templates)
  • When you don’t know what the type is (lambdas)
  • Don’t use it unnecessarily for return types like below:
1
auto spliceString(const string& s);
注意
auto doesn’t mean that the variable doesn’t have a type, it means that the type is deduced by the compiler.

相关内容