Skip to content

Instantly share code, notes, and snippets.

@endland
Last active October 17, 2020 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endland/5439eeceb15722e8326a9aebe97cc2ab to your computer and use it in GitHub Desktop.
Save endland/5439eeceb15722e8326a9aebe97cc2ab to your computer and use it in GitHub Desktop.
for loop
#include <iostream>
#include <vector>
int main() {
// In the standard library, a std::vector is an array with automatic size.
// Let's make a vector of ints and loop over the contents.
// The syntax for std::vector<> is discussed further in the lecture on template types.
std::vector<int> int_list;
int_list.push_back(1);
int_list.push_back(2);
int_list.push_back(3);
// Automatically loop over each item, one at a time:
for (int x : int_list) {
// This version of the loop makes a temporary copy of each
// list item by value. Since x is a temporary copy,
// any changes to x do not modify the actual container.
x = 99;
}
for (int x : int_list) {
std::cout << "This item has value: " << x << std::endl;
}
std::cout << "If that worked correctly, you never saw 99!" << std::endl;
return 0;
}
-----------------------------------------------------------------
#include <iostream>
#include <vector>
int main() {
std::vector<int> int_list;
int_list.push_back(1);
int_list.push_back(2);
int_list.push_back(3);
for (int& x : int_list) {
// This version of the loop will modify each item directly, by reference!
x = 99;
}
for (int x : int_list) {
std::cout << "This item has value: " << x << std::endl;
}
std::cout << "Everything was replaced with 99!" << std::endl;
return 0;
}
--------------------------------------------------------------------------
#include <iostream>
#include <vector>
int main() {
std::vector<int> int_list;
int_list.push_back(1);
int_list.push_back(2);
int_list.push_back(3);
for (const int& x : int_list) {
// This version uses references, so it doesn't make any temporary copies.
// However, they are read-only, because they are marked const!
std::cout << "This item has value: " << x << std::endl;
// This line would cause an error:
//x = 99;
}
return 0;
}
-----------------------------------------------------------
singed int와 unsigned int를 서로 비교 하지 않도록 주의! 보통 컨테이너의
사이즈 값은 unsigned int인 경우가 많음.
std::vector<int> v = {1,2,3,4};
for (int i=0; i < v.size(); i++) {
std::cout << v[i] << std::endl;
}
정상 동작 가능. 하지만..
std::vector<int> v = {1,2,3,4};
for (int i=0; i <= v.size()-1; i++) {
std::cout << v[i] << std::endl;
}
v의 사이즈가 0 인 경우 계산 값은 어마어마하게 큰 값. segfault유발.
std::vector<int> v;
for (int i=0; i <= v.size()-1; i++) {
std::cout << v[i] << std::endl;
}
-해법---------------------------------------------------
// Casting to signed int first helps to ensure that the result
// of subtraction will truly be a signed negative value when size is 0:
for (int i=0; i <= (int)v.size()-1; i++) {
// ...
}
// Rewriting the algebra to perform addition instead of subtraction
// helps to avoid going below 0:
for (int i=0; i+1 <= v.size(); i++) {
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment