for (char c = 0; c < 128; ++c) { ... } // Overflow!
for (int i = 0; i < 128; ++i) { ... } // OK!
// We accept on exception;
// you may ignore the signed/unsigned warning
// for this:
vector v;
for (int i = 0; i < v.size(); ++i) // Warning!, but It's OK!
On a normal PC, i<v.size()
warns of a problem that only happens if a vector has more than 231 elements.
long long ll_1 = 1000000; // 1,000,000
long long ll_2 = 1e6; // This case is more better!
long long ll_3 = 1'000'000; // This case is more better! (C++14)
This code, it looks like it will print same!
, but it actually prints 'NOT same!'. Why?
double a = 0.1 + 0.1 + 0.1;
double b = 0.3;
if (a == b)
cout << "same!\n";
else
cout << "NOT same!\n";
Floating point math is not exact. Simple values like 0.1 cannot be precisely represented using binary floating point numbers, and the limited precision of floating point numbers means that slight changes in the order of operations or the precision of intermediates can change the result. That means that comparing two floats to see if they are equal is usually not what you want (GCC even has a warning for this:
warning: comparing floating point with == or != is unsafe
).
// `epsilon zero` is approximately 8.854e-12
if (abs(a - b) < 1e-12)
cout << "same!\n";
int main() {
// 4 bytes X 1,024 X 1,024
// approximately 4MB, OverfloW!
int arr[1024][1024]; // error!
}
int arr1[1024][1024]; // OK!
int main() {
static int arr2[1024][1024] // OK!
}
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char
).
int arr[100][100];
// case 1. initialize array to 0
// Value 0 in memset is represented in binary as 0000 0000
memset(arr, 0, sizeof arr);
// case 2. initialize array to -1
// Value -1 in memset is represented in binary as 1111 1111
memset(arr, -1, sizeof arr);
// Within every element of the array
// is a value of 1111 1111 1111 1111 1111 1111 1111 1111
// That value is represented in int type as -1
// else
int size_i = sizeof arr / sizeof arr[0];
int size_j = sizeof arr[0] / sizeof(int);
for (int i = 0; i < size_i; ++i)
for (int j = 0; j < size_j; ++j)
arr[i][j] = 100;
// input: ABC DEF GHI
string s1, s2;
cin >> s1;
getline(cin, s2);
cout << s1 << '\n' << s2;
// s1: ABC
// s2: DEF GHI
cin
will skip any leading whitespace (tab spaces, carriage returns and blank spaces), then start reading when it comes to the first non-whitespace character and then stop reading when it comes to the next whitespace.
Trailing whitespace left in the input stream, so getline
extracts characters from input stream, which includes the whitespace.
In general, if you want to immediately throw away the whitespace that follows using cin
to not mess things up when you later use getline
, then use cin.ignore()
immediately after a cin
to discard the next value.
/*
cin.ignore() // skip a single character
cin.ignore(n, c) where n is an integer and c is a character.
In this form, characters are skipped until the number skipped
is equal to n or until the character c is encountered,
whichever comes first. If the character c is encountered, it
itself is not skipped. It just triggers the skipping to stop.
*/
// input: ABC DEF GHI
string s1, s2;
cin >> s1;
cin.ignore();
getline(cin, s2);
cout << s1 << '\n' << s2;
// s1: ABC
// s2: DEF GHI
// CP
#define CP do { \
std::ios::sync_with_stdio(false); \
std::cin.tie(NULL); \
} while (0)
#include <iostream>
// iostream
using std::cin;
using std::cout;
// ----------------------------------
int main() {
CP;
return 0;
}
// Template for the actual competition.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-std=c++14",
"-Wall"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "C/C++: g++ build active file",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
// Place your key bindings in this file to override the defaults
[
{
"key": "Ctrl+;",
"command": "terminal.focus",
"when": "editorFocus"
},
{
"key": "Ctrl+;",
"command": "workbench.action.focusActiveEditorGroup",
"when": "terminalFocus"
}
]
Editor: Auto Closing Brackets
Controls whether the editor should automatically close brackets after the user adds an opening bracket.
: never
이전 - 없음 | 목록 | 다음 - Array |
---|