Last active
October 17, 2016 02:41
-
-
Save cactorium/4d019023fd904d6f587edb83a7e32bb8 to your computer and use it in GitHub Desktop.
Fix typo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: if you are running in an IDE, you may need to add -std=c++11 | |
// to the compiler settings for this to compile correctly. | |
// GCC (which is the most common compiler suite) defaults to using an older standard of | |
// C++, so it won't be able to understand all the awesomecool features we're using here | |
// until you tell it that you want to use them | |
// | |
// A line that starts with two slashes like this is a comment! | |
// They're ignored by the compiler, so you can write whatever you want in them | |
// They're mainly used for commenting code, to better describe what's happening | |
// in the code if it's complicated | |
// Or for whatever else you want to use them for | |
// C++ by itself is very minimal, so you #include things so you have access to more | |
// stuff and libraries | |
// Here we include vector, which provides a vector type, and iostream, which lets | |
// you do I/O while reading/writing from the console | |
#include <vector> | |
#include <iostream> | |
// Like in C, when you make a C++ program, the program starts and ends in the main() | |
// function | |
// A function has a return type (here it's int, an integer), a name (main), | |
// and a optional list of parameters (which would by in the parenthesis; this main | |
// does not have any parameters) | |
int main() { | |
// Here we initialize an variable, which is an integer, named a with the value zero | |
// Now anywhere in this scope, inside main(), we can read, write, and | |
// do integer operations (like multiplication, addition, etc.) with a | |
// Initialization means creating a variable and giving it an initial value | |
// Variables should be initialized when they're created, because otherwise | |
// you have no idea what value they'll have when you use them | |
int a = 0; | |
// Here we create another variable, named vectorOfIntegers, which is a vector of integers | |
// std::vector<int>() calls the vector constructor to create a new vector | |
// the std::vector type is a generic type, so it takes in a type (here, int) | |
// to create a new type (std::vector<int>). This means a vector of integers. | |
// Generic types are kind of complicated and worth a lesson by themselves, in a lot | |
// of weeks after this one | |
// | |
// Vectors are like arrays, except they're automatically resize themselves | |
// They're a lot like a list, and it kind of helps to think of them that way | |
// You can add and remove stuff from a vector, and they stay in order as you do either | |
// of these | |
// | |
// as you add more and more elements (integers in this case) into them | |
// When you create complicated types like these, it's generally recommended that | |
// you don't include the type at the start of the declaration, and use auto | |
// instead | |
// auto basically means "let the compiler figure out the type", and it saves you | |
// the trouble of typing out std::vector<int> twice | |
// This matters a lot for more complicated types, which there are a lot of in the | |
// Standard Library | |
// By the way, we prefix vector with std because all the types in the Standard | |
// Library are in the std namespace | |
// Namespaces are kind of a place for code to live in | |
// This is so that there's less of a chance of name collisions, like if you wanted | |
// to make your own vector type, you could do it and you'd be able to tell the | |
// compiler which vector you meant | |
// If you wanted the one in the Standard Library, you'd write std::vector | |
// If you wanted the one you wrote, you'd write vector | |
// You can make your own namespaces, but for now we won't | |
// This is a lot of comments for a single line of code, my apologies! | |
auto vectorOfIntegers = std::vector<int>(); | |
// You read stuff in using std::cin, which is an input stream from the console | |
// (console input) | |
// Basically this line means "read whatever the user writes and put it into a" | |
// The nice thing about C++ is that you don't need to tell it that you want | |
// to convert the input into an integer (like you would in C if you were using | |
// scanf), C++ is smart enough to realize you're using an integer here, so it | |
// knows you want it converted into an integer | |
std::cin >> a; | |
// For loops are still a thing in C++ | |
// For those not versed in C, this line basically means | |
// "starting with i = 0, | |
// while i is less than a, | |
// do the stuff inside the braces | |
// then ++i (increase the value of i by one) | |
// and repeat" | |
// So the loop will start with i = 0, do the stuff inside the loop, then | |
// i = 1, do the stuff inside the loop, etc. until i < a is not longer true | |
// Essentially i will go from 0 to a-1, a total of a times | |
for (int i = 0; i < a; ++i) { | |
// std::cout is used for output. Note the that direction of the brackets | |
// is opposite of std::cin. This is meant to represent pushing data into | |
// std::cout, instead of pulling data from std::cin in a few lines up | |
// Here we pass in a string, a small section of text, to get printed on | |
// the console | |
std::cout << "enter a number: "; | |
// Here we initialize a new integer variable, named tmp this time | |
int tmp = 0; | |
// We get a value from the console | |
std::cin >> tmp; | |
// and then push it into the vector | |
// push_back() adds the integer to the end of the vector, expanding it by one | |
// So each time this is called the vector gets bigger, and the new value | |
// is added at the end of it | |
vectorOfIntegers.push_back(tmp); | |
} | |
// So overall, this loop uses the value we got from the user at the beginning | |
// and then takes in that number of integers from the user and stores them | |
// in the vector of integers called vectorOfIntegers | |
// So if the user input | |
// 3 | |
// 1 | |
// 2 | |
// 3 | |
// Then the vector will look like {1, 2, 3} | |
// Initializing another variable | |
int sum = 0; | |
// Here we're using another C++11 feature, ranged for loops | |
// These are for loops with a little bit nicer syntax than normal for loops | |
// They only work for some particular types, but fortunately vectors is one | |
// of the types you can use with them | |
// | |
// Basically you can read this as | |
// for all the elements of vectorOfIntegers, store the element in foo and do | |
// the stuff in the loop | |
// This doesn't modify the vector | |
// | |
// If you want a more mathematical reading, you can think of it as saying | |
// for foo in vectorOfIntegers | |
for (int foo: vectorOfIntegers) { | |
// sum += foo is the same as | |
// sum = sum + foo | |
// It's shorthand for adding to a variable | |
sum += foo; | |
} | |
// So overall we're adding all the integers in vectorOfIntegers into sum | |
// | |
// And then here we print it out for the user to see | |
// The std::endl is basically a newline; endl means "end line" | |
// You can also use "\n" and get essentially the same thing | |
std::cout << sum << std::endl; | |
// Because main() says it returns an integer, we return 0 | |
// which means that everything worked correctly | |
return 0; | |
// And we're done! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment