博文

[CPP] Try-Catch Clause

图片
As java, we need a try-catch block in c++. The universal format of try-catch is try{ program-statements } catch (exception-declaration) { handler-statements } catch (exception-declaration) { handler-statements } //... Standard exception C++ standard library defines a set of classes which is used for exception. They are defined in the following 4 header files: exception: defines the most general kind of exception class. It communicates only that an exception occurred but provides no additional information. stdexcept: defines several general-purpose exception classes. new: defines the bad_alloc exception type. type_info: defines the bad_cast exception type stdexcept: exception: the most general kind of problem runtime_error: problems that can be detected only at run time range_error: runtime_error: result generated outside the range of values that are meaningful overflow_error: runtime_error: computation that overflowed underflow_error: runtime_error: comp...

[LAC] Nondeterministic Finite Automata(NFA)

图片
A “nondeterministic” finite automata has the power to be in several states at once. This ability is often expressed as an ability to “guess” something about its input. For instance, when the automata is used to search for certain sequences of characters(e.g. keywords) in a long text string, it is helpful to “guess” that we are at the beginning of one of those strings and use a sequence of states to do nothing but check that the string appears, character by character. An informal view of NFA like the DFA, NFA has states, input symbols, transition functions, etc. The transition function in NFA is a function that takes a state and input symbol as arguments, but returns a set of zero, one or more states (rather than returning exactly one state, as the DFA must). Example of NFA: The job of NFA is to accept all and only the strings of 0’s and 1’s that end in 01. States q0 is the start state, and we can think of the automata as being in state q0 whenever it has not yet “guessed” . I...

[CPP] String, Vector and Iterator

图片
Library string type A string is a variable-length sequence of characters. To use the string type, we must include the string header. Because it is part of the library, string is defined in the std namespace: #include <string> using std::string; Defining and initializing strings examples: string s1; //default initialization; s1 is the empty string string s2 = s1; //s2 is a copy of s1, but it is copy initialization string s2(s1); //also the same as above. s2 is a copy of s1, but it is direct initialization string s3 = "hiya" //s3 is a copy of the string literal string s3("hiya"); //same as above string s4(10, 'c'); //s4 is cccccccccc operations on strings read and write strings int main(){ string s; cin >> s; cout << s << endl; return 0; } The string input operator reads and discards any leading whitespace(e.g. spaces, newlines, tabs). It then reads characters until the next whitespace character is encounte...

[CPP] Dealing with Types

图片
Type alias A type alias is a name that is a synonym for another type. Type aliases let us simplify complicated type definitions, making those types easier to use. We can define a type alias in one of two ways. Traditionally, we use a typedef ; typedef typedef double wages; // wages is a synonym for double typedef wages base, *p; // base is a synonym for double, p for double* The new standard introduced a second way to define a type alias, via an alias declaration Alias declaration using SI = Sales_item; //SI is a synonym for Sales_item An alias declaration starts with the keyword using followed by the alias name and an =. The alias declaration defines the name on the left-hand side of the = as an alias for the type that appears on the right-hand side pointers, const and type aliases Declarations that use type aliases that represent compound type and const can yield surprising results. For example, the following declarations use the type pstring , which is an alias for th...

[CPP] Variables

图片
Type Conversion A simple example: bool b = 42; //b is true int i = b; // i has value 1 i = 3.14; // i has value 3 double pi = i; // pi has value 3.0 unsigned char c = -1; // assuming 8-bit chars, c has value 255 signed char c2 = 256; // assuming 8-bit chars, the value of c2 is undefined Expressions Involving Unsigned Types For example, if we use both unsigned and int values in an arithmetic expression, the int value ordinarily is converted to unsigned. Converting an int to unsigned executes the same way as if we assigned the int to an unsigned(see the 32bits) example: Why in the first code, it is correct but in the second one “u+i” become so strange?! This is because int will always convert to unsigned int. In the first program, -9 convert to a relatively big number and add 10, then overflow appears again and it change to a positive value 1 instead. In the second program, there is no overflow, so it is the biggest unsigned int “va...

[LAC] Deterministic Finite Automata(DFA)

图片
As was mentioned before, a finite automation has a set of states, and its “control” moves from state to state in response to external “inputs” If the control is “deterministic”, means that the automata cannot be in more than one state at any one time. “non-deterministic”, means that it may be in several states at once. DFA The term “deterministic” refers to the fact that on each input there is one and only one state to which the automata can transition from its current state. A DFA consists of: A finite set of states , often denoted Q A finite set of input symbols, often denoted Σ A transition function that takes a state and an input symbol as arguments and returns a state. The transition function will commonly be denoted δ A start state , one of the states in Q A set of final or accepting state F. The set F is a subset of Q. Five tuple notation A = (Q, Σ, δ, q0, F) where A is the name of DFA Q is its set of states Σ is its input symbols ...

[CPP] Expressions

图片
Expressions are formed from these atomic elements: data and operators . Data may be represented either by variables or by constants. Like most other computer languages, C/C++ supports a number of different types of data. It also provides a wide variety of operations. Five basic data types There are five atomic data types in the C subset: char, int, float, double, and void The size and range of these data types may vary between processor types and compilers. In all cases a character is 1 byte. The size of an integer is usually the same as the word length of the execution environment, can be 16 bits or 32 bits. To the five basic data types defined by C, C++ adds two more: bool and wchar_t. Modifying the basic types You use a modifier to alter the meaning of the base type to fit various situations more precisely. The list of modifiers is shown here: signed unsigned long short When a type modifier is used by itself (that is, when it does not precede a basic type), the...