String char length()
String str a=str.length();
getline(cin , str)
Char str1[128],str2[4508]
cin.getline(str1,128);
cin.getline(str2,4508);
a=strlen(str1);
b=strlen(str2);
int_64 输入输出
scanf("%I64d",&a);
printf("%I64d",a);
__int 64 转化成 string
1.sstream
2.sprintf
3._i64toa
4.char *_i64toa
STL 字典序
Example
// next_permutation #include <iostream> #include <algorithm> using namespace std;
int main () { int myints[] = {1,2,3};
cout << "The 3! possible permutations with 3 elements:/n" ;
sort (myints,myints+3);
do { cout << myints[0] << " " << myints[1] << " " << myints[2] << endl; } while ( next_permutation (myints,myints+3) );
return 0; } |
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 |
1.C ++ 动态创建多维数组
char *a=new char[10];
delect [ ] a ; //[ ] 表示对数组空间的操作
正确的做法是先声明一个 n 维数组,每个单元是指向 char 的指针,再分别对每个单元分配内存 . 代码如下
char **array=new char*[n];
for(int i=0;i<n;i++)
array[i]=new char[n];
注意:上面代码在释放分配的内存时要特别注意。因为这是 “ 深度内存分配 ” ,所以释放时,要对每个单元里的指针指向的内存予以释放。释放内存代码如下:
for(i=0;i<n;i++)
delete[] array[i];
delete[] array;
一维 arr = (int *)malloc(sizeof(int)*n);
多维 char **p;
p = (char **)malloc(sizeof(char *) * n);
for(i = 0; i < n; i++)
p[i] = (char *)malloc(sizeof(char) * m);
释放:
for(i = 0; i < n; i++)
free(p[i]);
free(p);
C 语言排序
#include "stdio.h"
#include <stdlib.h>
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
void main()
{
int i,n,m;
int * arr;
scanf("%d",&n);
arr = (int *)malloc(sizeof(int)*n);
for(i=0; i<n; i++)
scanf("%d",&arr[i]);
qsort(arr,n,sizeof(int),compare); //C 语言排序
for(i=0; i<n; i++)
printf("%d%c",arr[i],' ');
printf("/n");
}
三阶矩阵相乘
for(i=0;i<3;i++;)
{
for(j=0;j<3;j++;)
{
for(k=0;k<3;k++;)
{
c[i][j] += a[i][k]*b[k][j]
}
}
}
十六进制转换为 2 进制
/* strtol example strtol("DF", NULL, 16);这样用*/
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd;
long int li1, li2, li3, li4;
li1 = strtol (szNumbers,&pEnd,10);
li2 = strtol (pEnd,&pEnd,16);
li3 = strtol (pEnd,&pEnd,2);
li4 = strtol (pEnd,NULL,0);
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld./n", li1, li2, li3, li4);
return 0;
}
/* itoa example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,buffer,10);
printf ("decimal: %s/n",buffer);
itoa (i,buffer,16);
printf ("hexadecimal: %s/n",buffer);
itoa (i,buffer,2);
printf ("binary: %s/n",buffer);
return 0;
}
Output:
Enter a number: 1750
decimal: 1750
hexadecimal: 6d6
binary: 11011010110