auto keyword1: #include <iostream>
2:
3: int main() {
4: std::cout << "Hello, world!" << std::endl;
5: }
g++ hello-world.cpp -o hello-world.out ./hello-world.out
main() with return type int gets run automaticallyvoid means no return/output)1: void addAndPrint(int x, int y) {
2:
3: }
1: void addAndPrint(int x, int y) {
2: int sum = x + y;
3: std::cout << sum << std::endl;
4: }
return statement must be used since the return type is not void anymore 1: void addAndPrint(int x, int y) {
2: int sum = x + y;
3: std::cout << sum << std::endl;
4: }
5:
6: int add(int x, int y) {
7: return x + y;
8: }
9:
10: void print(int value) {
11: std::cout << value << std::endl;
12: }
print(add(3, 4)) gives the same result as addAndPrint(3, 4)int foo(int x); ORint foo(int);int foo(int x) { return x+1; }1: // these all have different signatures
2: int add(int, int);
3: int add(int, int, int);
4: double add(double, double);
5: double add(double, double, double);
#include <iostream>?
<> gets code from the standard library or installed libraries"" gets code from a nearby folder, or does the same thing as <> if it can't find anything<> or "" is a file name
1: #include <string> // standard library
2: #include <QWidget> // file installed with Qt
3: #include <ros/ros.h> // file installed with ROS
4: #include "include/my_interface.hpp" // another file in the same project,
5: // in a folder called "include"
:: operator
std::string, my_library::MyClassstd::min_elementstd::string::nposMakeObject() returns some data of a particular typenamespace::MyVeryLongDataTypeName data = MakeObject();
OR
auto data = MakeObject();
This can make your code easier or harder to read/maintain, depending on whether you name your variables well
1: char[] s = "this is a string";
1: #include <string>
2: std::string s1("this is a string"); // constructor
3: std::string s2 = "this is a string"; // same effect as constructor
append(string), push_back(char)
std::cin >>
size() and length() each get number of charactersmake_palindrome
make_palindrome("apple") returns "appleelppa"1: RJRobot robot(REAL); // Make a new robot. Simulation may come later
2: robot.SetDriveMotors(200, -200); //-255 to 255 range on motors
3: robot.SetLiftMotor(127);
4: robot.Wait(1000ms);
5: robot.StopMotors();
6: int line_brightness = robot.GetLineValue(LightSensor::CENTER); // downwards line sensor
7: double clearance = robot.GetUltrasonicDistance(); // forwards ultrasonic sensor
8: Color ball_color = robot.GetColor(); // forwards color sensor. RED, BLUE, or UNKNOWN
Full interface defined in RJRobot.h. Use code completion in CLion as well.
software-training/hardware_applicationsspin_in_place)drive_in_square folder1 2 fizz 4 buzz fizz 7 8<algorithm> header contains min and max functions
max(4,7) -> 7