very basic STL

1
2
3
4
5
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}

std:::The STL

  • Tons of general functionality
  • Built in classes like maps, sets, vectors
  • Accessed through the namespace std::
  • Extremely powerful and well-maintained
    using namespace std; is not a good style!

types

more fundemental types: std::string str = "Haven";
C++: a interpreted language (Types before program runs during compilation)
C++ allows function overloading!
auto: a keyword that tell the compiler to deduce the type of a object or a varible

  • only use when the types are obvious
  • or when the type is annoyingly verbose to write out

structs

STL have its own structs!
std::pair: struct with 2 fields of any type(first and second)

initialization

1
2
3
int criticalSystemValue(42.5);
std::cout << "critial sys value" <<criticalSystemValue << endl;
return 0;

what is wrong?
42.5 is not a int, but C++ not care.
C++ not check with ini value, so it might cause narrowing conversation.

uniform ini (C++11)

int numOne{12.0}; with these uniform ini, C++ does care about types!
features:

  • safe: do not allow for narrowing conversions
  • ubiquitous: works for all types!

structures ini

1
2
3
4
5
6
7
struct stu {
string name;
string state;
int age;
}

stu s{'Haven', "AR", 21};

structure binding (C++17)

1
2
3
4
5
6
7
8
struct stu {
string name;
string state;
int age;
}
int main{
auto [name, state, age] = getClassInfo();
}

l-values and r-values

I-values(左值): can appear both in the right and left sides of the equations (not temp!)
r-values(右值): can appear only in the right hand side of the equations (temp.!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h> 
#include <cmath>
#include <iostream>
int squareN(int& num)
{
return std::pow(num, 2);
}
int main() {
int lvalue = 2;
auto four = squareN(1Value);
auto fourAgain = squareN(2); // wrong: 2 is a r-value: it cannot be passed to int&
std::cout << four << std::endl;
return 0;
}

const

to declare a value to be a r-value
cannot declare a non-const reference to a const value!

compiling

g++ -std=c++11 main.cpp -o main
g++ -std=c++11 main.cpp is also OK (resulting in a.out in linux)

Stream

a general input/output abstraction for C++
![[Pasted image 20240612182503.png]]
known as iostreams!
cout, cin: no need to say
cerr: output errors
clog: non-critical event logging
Input streams (I)

  • a way to read data from a source
    • Are inherited from std::istream
    • ex. reading in something from the console (std::cin)
    • primary operator: >> (called the extraction operator)
      Output streams (O)
  • a way to write data to a destination
    • Are inherited from std::ostream
    • ex. writing out something to the console (std::cout)
    • primary operator: << (called the insertion operator)

stringstreams

a way to treat string as streams

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {
/// partial Bjarne Quote
std::string initial_quote = “Bjarne Stroustrup C makes it easy to shoot
yourself in the foot”;
/// create a stringstream
std::stringstream ss(initial_quote);
/// data destinations
std::string first;
std::string last;
std::string language, extracted_quote;
ss >> first >> last >> language; //first = "Bjarne", last = "Stroustrup", language = "C"
std::getline(ss, extracted_quote);
std::cout << first << “ ” << last << “ said this: ”<< language << “ “ <<
extracted_quote << std::endl;
}

what if we want the extracted_quote to be the rest? the >> only read till the blank, so we tend to use getline()!

output streams

std::endl: tells the cout stream to end the line!
also tells the stream to flush(冲厕所)

1
2
3
4
5
6
7
int main()
{
for (int i=1; i <= 5; ++i) {
std::cout << i << ‘\n’;
}
return 0;
}

buffer: 5 ‘\n’

1
2
3
4
5
6
7
int main()
{
for (int i=1; i <= 5; ++i) {
std::cout << i << ‘\n’;
}
return 0;
}

buffer:
1 ‘\n’ 2 ‘\n’ 3 ‘\n’ 4 ‘\n’ (until it is full, it releases)
5 '\n'

file systems

std::ofstream

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
/// associating file on construction
std::ofstream ofs(“hello.txt”) // create a output file stream to the file hello.txt
if (ofs.is_open()) { // check if the file is open
ofs << “Hello CS106L!” << ‘\n’;
}
ofs.close(); // close the output stream to hello.txt
ofs << “this will not get written”;
ofs.open(“hello.txt”); // open the stream again
ofs << “this will though! It’s open again”;
return 0;
}

input streams

cin reads up to white space
when cin fails: it cannot find the corresponding value until the next whitespace!
In conclusion:

  1. Streams are a general interface to read and write data in programs
  2. Input and output streams on the same source/destination type compliment each other!
  3. Don’t use getline() and std::cin() together, unless you really really have to!