template <class T>
void Swap(T&x, T&y) {
T tmp = x;
x = y;
y = tmp;
}
int main() {
int n = 1, m = 2;
Swap(n, m);
double f = 1.2, g = 2.3;
Swap(f, g);
return 0;
}
template <class T>
T Max(T a, T b)
{
cout << "Template Max 1" << endl
;
return 0;
}
template <class T, class T2>
T Max(T a, T2 b)
{
cout << "Template Max 2" << endl
;
return 0;
}
double Max(double a, double b) {
cout << "MyMax" << endl;
return 0;
}
int main()
{
int i = 4, j = 5;
Max(1.2, 3.4); //调用Max(double, double)函数
Max(i, j); //调用第一个T Max(T a, T b)模板生成的函数
Max(1.2, 3); //调用第二个T Max(T a, T2 b)模板生成的函数
return 0;
}
template<class T1,class T2>
class A {
T1 v1; T2 v2;
};
template<class T1, class T2>
class B :public A<T2, T1> {
T1 v3; T2 v4;
};
template<class T>
class C :public B<T, T> {
T v5;
};
int main() {
B<int, double> obj1;
C<int> obj2;
return 0;
}
2. 类模板从模板类派生:
template<class T1,class T2>
class A {
T1 v1; T2 v2;
};
template<class T>
class B :public A<int, double> {
T v;
};
int main() {
B<char> obj1;
return 0;
}
自动生成两个模板类:A<int,double>和B<char>
3. 类模板从普通类派生
class A { int v1; };
template <class T>
class B :public A { T v; };
int main() {
B<char> obj1;
return 0;
}
4. 普通类从模板类派生:
template <class T>
class A { T v1; int n; };
class B:public A<int> { double v; };
int main() {
B obj1;
return 0;
}
asort() and arsort() — For sorting associative arrays by value
ksort() and krsort() — For sorting associative arrays by key
Round a Fractional Value up or down
ceil(): round a fractional value up;
floor(); round a fractional value down;
rand(); generate some random numbers; //rand(1,10)
PHP Get and Post:
<!DOCTYPE html><htmllang="en"><head><title>Example of PHP GET method</title></head><body><?phpif(isset($_GET["name"])){echo"<p>Hi, ".$_GET["name"]."</p>";}?><formmethod="get"action="<?phpecho$_SERVER["PHP_SELF"];?>"><labelfor="inputName">Name:</label><inputtype="text"name="name"id="inputName"><inputtype="submit"value="Submit"></form></body>
Note: $_SERVER["PHP_SELF"] call the php inside this URL;
Running PHP code on Terminal window: sudo php -f filename.php
Login mysql: mysql -u root -p
Rename file in Linux: sudo mv original_filename new_filename
Building the register system: Step1: creating the database table CREATETABLE users (
id INTNOTNULLPRIMARYKEYAUTO_INCREMENT,
username VARCHAR(50)NOTNULLUNIQUE,
password VARCHAR(255)NOTNULL,
created_at DATETIMEDEFAULTCURRENT_TIMESTAMP); INT: Stores numeric values in the range of -2147483648 to 2147483647 NOT NULL: constraint the field cannot accept a NULL value. PRIMARY KEY: constraint; marks the corresponding field as the table's primary key. AUTO_INCREMENT: a MySQL extension to standard SQL, which tells MySQL to automatically assign a value to this field if it is left unspecified, by incrementing the previous value by 1. Only available for numeric field. VARCHAR():Stores variable-length strings with a maximum size of 65,535 characters. N:CHAR():Stores fixed-length strings with a maximum size of 255 characters. UNIQUE: ensures that each row for a column must have a unique value. DATETIME: Stores combined date/time values in the YYYY-MM-DD HH:MM:SS format. CURRENT_TIMESTAMP: Stores timestamp values. TIMESTAMP values are stored as the number of seconds since the Unix epoch ('1970-01-01 00:00:01' UTC). Create the Config File to connect the MySQL database server. <?php/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */define('DB_SERVER','localhost');define('DB_USERNAME','root');define('DB_PASSWORD','123456');define('DB_NAME','demodb');/* Attempt to connect to MySQL database */$link=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);// Check connectionif($link===false){die("ERROR: Could not connect. ".mysqli_connect_error());}?> Create the Registration Form. <?php// Include config filerequire_once"config.php";// Define variables and initialize with empty values$username=$password=$confirm_password="";$username_err=$password_err=$confirm_password_err="";// Processing form data when form is submittedif($_SERVER["REQUEST_METHOD"]=="POST"){// Validate usernameif(empty(trim($_POST["username"]))){$username_err="Please enter a username.";}else{// Prepare a select statement$sql="SELECT id FROM users WHERE username = ?";if($stmt=mysqli_prepare($link,$sql)){// Bind variables to the prepared statement as parametersmysqli_stmt_bind_param($stmt,"s",$param_username);// Set parameters$param_username=trim($_POST["username"]);// Attempt to execute the prepared statementif(mysqli_stmt_execute($stmt)){/* store result */mysqli_stmt_store_result($stmt);if(mysqli_stmt_num_rows($stmt)==1){$username_err="This username is already taken.";}else{$username=trim($_POST["username"]);}}else{echo"Oops! Something went wrong. Please try again later.";}}// Close statementmysqli_stmt_close($stmt);}// Validate passwordif(empty(trim($_POST["password"]))){$password_err="Please enter a password.";}elseif(strlen(trim($_POST["password"]))<6){$password_err="Password must have atleast 6 characters.";}else{$password=trim($_POST["password"]);}// Validate confirm passwordif(empty(trim($_POST["confirm_password"]))){$confirm_password_err="Please confirm password.";}else{$confirm_password=trim($_POST["confirm_password"]);if(empty($password_err)&&($password!=$confirm_password)){$confirm_password_err="Password did not match.";}}// Check input errors before inserting in databaseif(empty($username_err)&&empty($password_err)&&empty($confirm_password_err)){// Prepare an insert statement$sql="INSERT INTO users (username, password) VALUES (?, ?)";if($stmt=mysqli_prepare($link,$sql)){// Bind variables to the prepared statement as parametersmysqli_stmt_bind_param($stmt,"ss",$param_username,$param_password);// Set parameters$param_username=$username;$param_password=password_hash($password,PASSWORD_DEFAULT);// Creates a password hash// Attempt to execute the prepared statementif(mysqli_stmt_execute($stmt)){// Redirect to login pageheader("location: login.php");}else{echo"Something went wrong. Please try again later.";}}// Close statementmysqli_stmt_close($stmt);}// Close connectionmysqli_close($link);}?><!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Sign Up</title><linkrel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css"><styletype="text/css">body{font: 14px sans-serif;}.wrapper{width: 350px;padding: 20px;}</style></head><body><divclass="wrapper"><h2>Sign Up</h2><p>Please fill this form to create an account.</p><formaction="<?phpechohtmlspecialchars($_SERVER["PHP_SELF"]);?>"method="post"><divclass="form-group <?phpecho(!empty($username_err))?'has-error':'';?>"><label>Username</label><inputtype="text"name="username"class="form-control"value="<?phpecho$username;?>"><spanclass="help-block"><?phpecho$username_err;?></span></div><divclass="form-group <?phpecho(!empty($password_err))?'has-error':'';?>"><label>Password</label><inputtype="password"name="password"class="form-control"value="<?phpecho$password;?>"><spanclass="help-block"><?phpecho$password_err;?></span></div><divclass="form-group <?phpecho(!empty($confirm_password_err))?'has-error':'';?>"><label>Confirm Password</label><inputtype="password"name="confirm_password"class="form-control"value="<?phpecho$confirm_password;?>"><spanclass="help-block"><?phpecho$confirm_password_err;?></span></div><divclass="form-group"><inputtype="submit"class="btn btn-primary"value="Submit"><inputtype="reset"class="btn btn-default"value="Reset"></div><p>Already have an account? <ahref="login.php">Login here</a>.</p></form></div></body></html>