Some knowledge of c ++

1. Input and output

There must be #include <iostream> using namespace std;
1. Enter:

 cin >>变量1;
   cin >>变量1>>变量2>>变量n;

2. Output:

cout <<表达式<<endl;

cout <<表达式1<<表达式2<<<<表达式n;

cout <<endl;//换行;

For example, the output of Hello world:

#include <iostream>
using namespace std;
int main()
{
 cout<<"hello world"<<endl;
}

3. String
1. Class name string is used to define string variables, for example:

string string1;

Use strcat to connect strings in 2.c, + for C ++. E.g:

string3 = string2 +string1;

3.c will use the function strcpy to assign string2 to string3, c ++ uses = directly.

string1 = string2;

4. String comparison directly uses relational operators such as>, <,> =, <=, ==. It returns 1 if it meets the condition, and returns 0 if it does not.
5. C ++ commonly used string functions

function function
append Add characters to the end of the string
at Returns a reference to the element at the specified position in the string
c_str Convert the contents of the string to a C-style null-terminated string
data The contents of the string converted to a character array
empty Test whether the string contains characters
erase Remove an element or a range of elements from the specified position string
find Forward search string for the first match of the substring that matches the specified character sequence
find_first_not_of Search for the first character string matching any specified string element
find_first_of Search for the first character string matching any specified string element
length Returns the current number of elements in the string

2. Stack and Queue

1 Stack (last in, first out).
1. Need header file #include<stack>
2. Stack : push ()
form: s.push (1);
3. Stack out: pop ()
form: s.pop ();
Note: stack out The operation did not return any value.
4. Access to the top element of the stack: top ()
form: cout << s.top () << endl;
4. Determine whether it is empty: empty ()
form: cout << s.empty () << endl; if it is empty It returns 1, if it is not empty, it returns 0.
5. Find the number of elements in the stack: size ()
format: cout << s.size () << endl;
6. Take the top of the stack: s.top () ; Take the top of the stack to return to the top element of the stack.
2. Queue
1. Define a priority queue: priority_queue pq; Similar to the stack
2. Determine whether a queue is empty: empty ();
3. Dequeuing the head element: pop ();
4. Insert an element: push () ;
5. Return the number of elements in the queue: size ();
6. Return the top element of the priority queue: top ();

#include<bits/stdc++.h>
#include<stack>
#include<queue>
using namespace std;

int main() {
    stack<int>s;  //定义栈 
    queue<int>q;  //定义队 
    }

Simple application:
[Example] Convert decimal numbers to binary numbers.
Answer: Press 2 to take the remainder.
Analysis: Since the residue obtained first is the lowest bit of the conversion result, and the residue obtained last is the highest bit of the conversion result, it is solved with the stack.

#include<bits/stdc++.h>
#include <STACK>
using namespace std;
void conversion(int N)
{	
	stack<int> S;			
	while(N)
	{
			S.push(N%2);		
	   		N=N/2;	
	   }
	   while(!S.empty())
	  {		
	  	printf("%d",S.top());
	  	S.pop();	
	  }
}

int main()
{	
	cin>>n;
	fun(n,2);
}
发布了10 篇原创文章 · 获赞 2 · 访问量 217

Guess you like

Origin blog.csdn.net/dfwef24t5/article/details/104350949