C ++ some small knowledge

1.cout.precision (n); retention n significant digits , and the automatic function 0 discard excess,
in an iostream
2.cout.setf (ios :: fixed); decimal display , rather than scientific notation, but will useless 0 whole
show, a lot more than one function
3. cout << setprecision (3) << x << endl; display three decimal time but used to pay attention to the need to include the header file #include<iomanip>in order to use
4. the atof () function to convert the string into a double , there will be a corresponding string into an int atoi function
program

#include <iostream>
#include<stdlib.h>
#include<string>
using namespace std;
int main()
{
	string s("1234");
	cout<<"s= "<<s<<endl;
	float a;
	a=atof(s.c_str());
	cout<<"a= "<<a<<endl;
}

To add a semicolon complete class definition
class definition to add a semicolon complete
class definition to add a semicolon finished
compiling check out the

6. Use stringstream concatenate strings and numbers
must pay attention to have
first use of ss >> s3;. When it came to space stopped, it is ss received data to from the beginning to the first space output S3;
second .ss.clear (); previous input not clear, effective approach is ss.str ( "");
. the third output data including spaces can all ss received string to a method is s3 = ss.str ();
exemplary procedure is as follows

#include<string>
#include<sstream>
using namespace std;
int main()
{
	int index=1;
	string s1="We_love";
	string s2=" game!";
	stringstream ss;
	ss<<s1<<index<<s2;
	string s3;
	ss>>s3;
	cout<<"s3 = "<<s3<<endl;

	cout<<"next"<<endl;
	
	ss.str(" ");
	index=2;
	ss<<s1<<index<<s2;
	s3=ss.str();
	cout<<"s3 = "<<s3<<endl;
}

The results for the program s3 = We_love1 next s3 = We_love2 game!
to another approach to achieve similar functionality

#include<string>
int i=100;
string s="/rgb/"+to_string(i)+" ,png ";

7.c ++ of one chronograph method

#include<iostream>
#include<chrono>
using namespace std;

int main()
{
	cout<<"开始记时"<<endl;
	chrono::steady_clock::time_point t_start = chrono::steady_clock::now();
	for(double i=0;i<10000;i++)
		for(double j=0;j<10000;j++);
	chrono::steady_clock::time_point t_end = chrono::steady_clock::now();
	chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t_end-t_start);
	cout<<"共用时 : "<<time_used.count()<<" seconds ."<<endl;
	
}

But if the direct use g ++ compiler will be prompted to roughly meaning for the need to support error c ++ 11, and is used here to solve cmake

PROJECT(main)#设置工程名
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)#设定版本

list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules )

SET(CMAKE_CXX_COMPLILER "g++")#设定编译器
SET( CMAKE_BUILD_TYPE Release  )
SET( CMAKE_CXX_FLAGS "-std=c++11 -O3")

add_executable(main main.cpp)

8. rounding
Floor (10.5) = 10; ceil (10.5) =. 11;
Floor (-10.5) = -. 11; ceil (-10.5) = 10;
Floor is rounding down, ceil is rounded up
round (x ) returns an integer value of x rounded.

Guess you like

Origin blog.csdn.net/qq_34122731/article/details/86634019