1.
char*與char[]區別:
char* s1=”abc”;s1是一個指針,s1所指向的地址的內容是不可改變的,但是s1可以指向其他地址。s1是指向字符串常量的,它存儲在裏不可被修改
char* s1="abcd";
s1[2]='z'; //錯誤:編譯時能通過運行的時候會報錯
s1="xyz"; //可以將指針指向其他內容
cout<<s1[2]<<endl;
char s2[] =”cdef”;是一個數組,s2指向第一個元素所在的位置,一經分配就不能更改。 它的空間是則棧裏分配的,可以被重新修改,但是s2不能夠再指向其他空間
char s2[]="efgh";
s2="xyz"; //出錯:s2不可以再指向其他內容
cout<<s2[2]<<endl; // s2中的元素是可以被修改的
若是將指針指向一個數組,那麼這個數組即可以被改變元素值又可以被指向其他字符串
char *p=s2;
p[0]='x'; //可以改變元素值
p="rty"; //可以指向其他字符串
cout<<p;
char *與char a[] 的本質區別:
1)當定義char a[10]時,編譯器會給數組分配十個單元,每個單元的數據類型爲字符,sizeof(a) = 10
2)而定義char *s 時,這是給指針變量,只佔四個字節,32位,用來保存一個地址。sizeof(s) = 4
printf("%p", s); // 這個表示s的單元中所保存的地址
printf("%p", &s); // 這個表示變量本身所在內存單元地址,不要搞混了
注:
(1).
char *s1:s1是一個指向字符串的指針;
char s2[]:s2是一個字符數組;
string s3:s3是一個string類的對象.
(2).
string和CString是類,有特定的構造函數和一些方法方便你操作字符串。而char是一種度原始數據類型,可以是一個字符也可回以是字符數組像char[] 、char*,它不是類,所以只能藉助現成的libc中的函數來操作它,比如strcpy strcmp strlen等。
(3).
char (*s)[i]與char *s[i]區別:
char (*s)[i]; //s是個指針,它指向長度是i的一維數組,可以用第二維長度是i的二維數組的地址初始化它;
char *s[i]; //s是個數組,它的元素是指向字符類型的指針char* .
(4). 取char a[20] ,char *s ,當s=a ,則s[i] == a[i]
例:
#include<iostream>
using namespace std;
int count_last(char* str)
{
int count = 0;
if(str == NULL)
return 0;
int i = 0;
while(str[i] != '\0') // \0是組的結束字符標誌
i++;
int length = i;
while(str[i-1] != ' ' && (i >= 1)) //從尾向前
i--;
count = length - i;
return count;
}
int main()
{
char str[5000];
int count = 0;
cin.getline(str, 5000);
count = count_last(str);
cout << count;
return 0;
}
2.
例
#include<iostream>
using namespace std;
#include<string>
int Chazhao(string str, char ch)
{
int N = 0;
if(ch >= 'A' && ch <= 'Z')
ch = ch +32;
for(int i = 0; i < str.length(); i++)
{
if(str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
if(str[i] == ch)
N++;
else
continue;
}
return N;
}
int main()
{
string ip;
char zifu;
getline(cin, ip);//輸入字符串
zifu = getchar();//輸入字符
int N = Chazhao(ip, zifu);
cout << N << endl;
return 0;
}
3.
vector向量:是一個能夠存放任意類型的動態數組
介紹 https://blog.csdn.net/guoyiyan1987/article/details/80325782
例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
vector<int> OperateInput(int size, vector<int> arr)
{
vector<int> res;
for(int i = 0; i < size; i++)
{
int temp = arr[i];
for(int j = i + 1; j < size; j++)
{
if(temp == arr[j])
arr[j] = -1;//重複元素作標記-1
}
}
for(int i = 0; i < size; i++)
if(arr[i] != -1)
res.push_back(arr[i]);
sort(res.begin(), res.end());
return res;
}
int main()
{
int size;//個數
while(cin >> size)
{
vector<int> arr;//動態數組
int temp;
for(int i = 0; i < size; i++)
{
cin >> temp;
arr.push_back(temp);
}
vector<int> result = OperateInput(size, arr);
for(int j = 0; j <result.size(); j++)
cout << result[j] << endl;
}
return 0;
}
4.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
while(getline(cin, s1))
{
getline(cin, s2);//連續輸入2個字符串
if(s1.empty() == 0)//非空
{
if(s1.size()%8 != 0)
s1.insert(s1.size(), 8 - s1.size()%8, '0');//在尾部補足夠的0
for(int i = 0; i < s1.size(); i += 8)
cout << s1.substr(i, 8) <<endl;//複製子字符串,從指定位置i開始,按指定的長度8
}
if(s2.empty() == 0)//非空
{
if(s2.size()%8 != 0)
s2.insert(s2.size(), 8 - s2.size()%8, '0');//在尾部補足夠的0
for(int i = 0; i < s2.size(); i += 8)
cout << s2.substr(i, 8) <<endl;//複製子字符串,從指定位置i開始,按指定的長度8
}
}
return 0;
}
5.
#include <iostream>
using namespace std;
int main()
{
long num;
cin >> num;
while(num != 1)
{
for(int i = 2; i <= num; i++) //不能是i<num
{
if(num%i == 0)
{
cout << i << ' ';
num = num / i;//i是質數之一,並把num縮小
break;//此次for結束,再次while
}
}
}
return 0;
}
6.
C++中常用的兩種創建動態數組方法:new()和vector.
vector是c++標準庫中定義的類型,是容器的一種。標準庫中容器有很知多種,vector只是最基本的一種,vector類型和數組類型的道基本功能都是一樣的,就是存儲同類元素,但是他與數組最大的區別就是可以實現動態存儲。而new是關鍵字,用於動態分配內存,分配的是棧空間,要用delete來釋放,否則會出現內存泄露。二者說不上什麼區別,不是一樣的東西。vector是一種類型,定義該容器類型用到了new。
https://blog.csdn.net/cuiy0818/article/details/81503172
例
#include <iostream>
using namespace std;
int main()
{
int num;//鍵值對數
cin >> num;
int count = num;
int* index = new int[num];//鍵 數組
int* value = new int[num];//值 數組
for(int i = 0; i < num; i++)
cin >> index[i] >> value[i];
int j = 0;
while(j < num - 1)
{
if(index[j] < num)
{
for(int i = j + 1; i < num; i++)
{
if(index[i] == index[j])
{
value[j] += value[i];
index[i] = num;
count--;
}
}
}
j++;
}
int min, temp;
for(int i = 0; i < num - 1; i++)
{
min = i;
for(int m = i+1; m < num; m++)
if(index[min] > index[m])
min = m;
if(min != i)
{
temp = index[min];
index[min] = index[i];
index[i] = temp;
temp = value[min];
value[min] = value[i];
value[i] = temp;
}
}
for(int i = 0; i < count; i++)
cout << index[i] << ' ' << value[i] << endl;
delete [] index;
delete [] value;
return 0;
}
7.
vector容器find()、erase()、unique()、insert():
unique”刪除“不是真正的刪除,即不像erase那樣把元素刪除,而是把元素移到下標最末端,要注意unique返回給的迭代器是指向非重複元素的下一個地址,即重複元素的首地址。(使用unique函數一般先對容器的值排序)
https://blog.csdn.net/clz16251102113/article/details/81138137
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
例
#include <iostream>
#include <vector>
using namespace std;
#include <algorithm>
#include <string>
int main()
{
string str;
vector<char> vec;
while(cin >> str)
{
for(int i = 0; i < str.size(); i++)
{
if(str[i]>=0 && str[i]<=127)
vec.push_back(str[i]);
}
sort(vec.begin(), vec.end());//排序
vec.erase(unique(vec.begin(), vec.end()), vec.end());//去重
cout << vec.size() << endl;
}
return 0;
}
8.
字符串輸入時,cin與getline()區別:
cin在接受字符串時,遇“空格”、“TAB”、“回車”都結束,getline()就沒有這問題。
https://www.cnblogs.com/renzhuang/articles/6993689.html
例1
例2
9.
例
#include <iostream>
using namespace std;
#include <string>
#include <stack>
int main()
{
string s;
stack<string> ss;//取該棧以單詞爲元素單位
while(cin >> s)//因爲各詞之間需要用空格來間隔開,所以此處不能用getline()
ss.push(s);//入棧
while(!ss.empty())
{
cout << ss.top();
cout << ' ';
ss.pop();
}
return 0;
}
10.
C++ 中的&:“&引用” 和“&取地址符”的區別和作用
引用就是某一變量(目標)的一個別名,對引用的操作與對變量直接操作完全一樣。
注意以下例子用和不用’&引用‘的區別:
int function(int i){
i++
return i;
}
int functionReference(int &i){
i++;
return i;
}
int x = 5;
function(x); //返回值:6, x :5
functionReference(x); //返回值:6, x :6
例
#include <iostream>
#include <string>
using namespace std;
#include <algorithm>
bool Compare(string &s1, string &s2)//&引用
{
return s1 < s2;
}
int main()
{
int n;
vector<string> res;
while(cin >> n)
{
while(n != 0)
{
n--;
string m;
cin >> m;
res.push_back(m);
}
sort(res.begin(), res.end(), Compare);
for(int i = 0; i < res.size(); i++)
cout << res[i] << endl;
}
return 0;
}
11.
普通變量、引用變量和指針變量作函數形參的區別:
三者都是傳值。普通變量時,不影響實參;引用變量時,可修改實參值;指針變量時,不影響指針本身,但通過指針可影響其指向的變量的值。所以,在C++中常用引用做形參,當不需要實參進行改變的時候在引用前加上“const”進行限制就可以了。