Essential Bash Commands and C++ Fundamentals
Essential Bash Commands
- Use which -a to find the path to the Bash shell.
- Use echo -n to avoid trailing newlines.
- ls -l lists the content in long format.
- ls -a lists all content, including hidden files.
- ls -la combines both.
- pwd prints the current directory.
- Use man x to view the manual of command x.
- Use ~ to navigate to the home directory.
- mkdir creates directories.
- Multiple directories can be created at once by typing them in the same line.
- rmdir deletes an empty directory.
- rm -rf deletes a non-empty directory.
- mv dir dir1 renames the directory dir to dir1.
- touch creates an empty file.
- cat displays a file.
- mv filename directory name moves the file to the directory.
- Multiple files with the same prefix can be referred to using (*).
- Use chmod [who][operator][permissions] filename to change the permissions of a file. U represents user, G represents group, O represents other, and A represents all.
- Example: chmod o+w file
- grep ‘parameter’ filename searches a file. Use single or double quotes. It is case-sensitive.
- wc filename gives word count information, including lines, words, and bytes.
- Use wc -l to count lines, wc -w for words, and wc -m for character count.
- sort filename sorts files in alphabetical order.
- Use sort -n for numeric sort and sort -r for reverse order.
- Use sort -k(field number) for sorting by a specific field.
- Use -t to specify the field separator if it is not space.
- Use cut -d ‘delimiter’ -f specify,columns filename to select specific columns from a file separated by a delimiter.
- uniq removes adjacent duplicates.
- Use find [path] [-name] [-type] to search for files or directories.
- Use -type f to search for files and -type d to search for directories.
- Use head -n 15 filename to select the first fifteen lines of a file.
- Use cp to copy files, -r for directories.
C++ Fundamentals
C++ is case-sensitive, meaning that variables with different capitalizations are treated as separate entities. For example, radius, RADIUS, and Radius are all different variables. Another thing to note is that a character is enclosed within single quotes (‘ ‘), while a string is enclosed by double quotes (” “). If you want to output a backslash, you need to use ‘\\’ instead. To make a variable constant, add the const modifier in front of it. The modulus operator (%) gives us the remainder of a division operation but cannot be used with double values. If both operands of an integer division operation are integers, the result will not include any fractional part. For example, if we have int a=3, b=2; cout<, the output will be 1.
We can use arithmetic operations with characters, as the ASCII values of the characters will be used. The ASCII value of “a” is 97, while the ASCII value of “A” is 65. Logical operators in C++ include “&&” (and), “||” (or), and “!” (not). Precedence is given to “!” over “&&”, and “&&” over “||”. Type conversions in C++ involve promoting a “lower” type to a “higher” type. For example, “double/int=double”. When assigning values, the value on the right side is converted to the type on the left. For example, double x=5;
assigns the value 5.0 to x. If you convert a double to an int, the fraction part will be removed, so int x=2.8;
assigns the value 2 to x. To convert types, you can use (int) 2.8
to send a message to the compiler that it’s an intended type conversion.
When taking multiple inputs with one line of code, you should use cin<, not
cin<. File redirection in C++ involves using
./exe_file_nameoutput_txt_file_name
. Note that the output file is rewritten every time. Switch statements only work with integer values. An alternative to if-else statements is cout<<((condition)? True_expr:false_expr);
. If you want to pass a variable by reference, put "&" before the variable name. For example, int &x
.
In C++, arrays should be enclosed within {} brackets. If an array is initialized in its declaration, the size of the array may be omitted, and the array will automatically be declared to have the minimum size needed for the initialization values. You can’t initialize or change the content of the whole array using an equal sign after its declaration. When a 2D array parameter is given in a function header or function declaration, the size of the first dimension is not given, but the remaining dimension size must be given in square brackets.
C++ String Functions
C++ has several string functions, including:
getline(cin,name)
s.size()
s.length()
s.empty()
s[index]
s.at(index)
s.compare(t)
s.find(pattern,start)
s.rfind(pattern,start)
s.find_first_of(charset,start)
orfind(str,pos)
s.find_last_of(charset,start)
orrfind(str)
s.substr(start)
s.substr(start,num)
s[index]=any value
s.append(t)
s+t
s.insert(index,t)
s.insert(index,t,num)
s.erase(start)
s.erase(start,num)
s.replace(index,num,t)