操作内存,指针再理解
#include<iostream>
using namespace std;
int main()
{
//内存分配
void *a1=malloc(1000);
//写入字符串
int *a2= (int*)a1;
*a2= (int)"这是写入的字符121212121212121212121212";
//强制指针类型转换
cout<< (char*)*(int*)a1 <<endl;
//写入整数
*a2=1000121;
int *a3 = (int*)a1;
cout << "指针中保存的地址(变量的地址) " << a3 <<endl;
cout << "取指针中保存的地址的值 (变量的值) " << *a3 <<endl;
cout << "取指针的地址指针 (指针自身的地址) " << &a3 <<endl;
cout << "对指针中的值 取地址 和 直接访问指针得到的都是 变量的地址 " <<&(*a3) <<endl;
}
运行结果
多重指针
int a[2];
string s1="asfa",s2="qwqwqw";
a[0]=(int)&s1;
a[1]=(int)&s2;
int i1=1212;
int i2=4444;
int* point1=&i1;
int** point2=&point1;
*point1=222;
cout<< *(string*)(a[1]) <<endl;
cout << typeid(point1).name() << endl;
cout << *point1 <<endl;
cout << i1 <<endl;
**point2=345;
int* point12=&i2;
point2=&point12;
**point2=5555;
cout << i1 <<endl;
cout << i2 << endl;
手动指针访问数组
#include<iostream>
using namespace std;
int main{
//定于整形数组,循环访问他们
int intlist1[]={76, 77, 75, 33};
for(int i=0;i<4;i++){
cout << &intlist1[i] << " " << intlist1[i] <<endl;
}
// 采用 int * 指针访问他们
int* intPoint1=intlist1;
for(int i=0;i<4;i++){
cout<<"int* 访问" <<*(intPoint1++) <<endl;
}
// 采用 void * 空指针访问他们
void* voidPoint1=intlist1;
int ontIntLenth= sizeof(int);
for(int i=0;i<4;i++){
cout<< "void* 访问" << *(int*)(int(voidPoint1)+i*ontIntLenth) <<endl;
}
// 采用 char* 指针访问他们
char* charPoint1=(char*)intlist1;
for(int i=0;i<4;i++){
cout<< "char* 访问" << charPoint1+i*ontIntLenth <<endl;
}
//关于 char* 的一些特殊说明
// 关于 char string1[]="xxxxxxx"
// 以及 char* 的区别
// 所以以后千万不要再单独使用 char *
// 因为 char * 默认不可修改, 要使用时,务必在前面加上 const 提醒自己以及别人
// 截取字符
char charP2[]="KKK!111111111111111111111111111111";
cout<< charP2 <<endl;
char* charP3=charP2;
int oneCharLen= sizeof(char);
charP3+=5*oneCharLen;
const char * temp1="\0";
*charP3=*temp1;
cout<< charP2 <<endl;
}
快速加载整个文件
// 根据文件路径 读取文件 返回包含文件内容的字符串
std::string getStringFromFile(string filepath){
std::ifstream in(filepath);
std::istreambuf_iterator<char> begin(in);
std::istreambuf_iterator<char> end;
std::string some_str(begin, end);
if(some_str==""){
std::cout << "open-file-error" <<endl;
}
return some_str;
}
程序运行计时
#include<iostream>
#include<ctime>
using namespace std;
void showruntime(clock_t startTime, clock_t endTime){
cout << "The run time is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
}
int main(){
clock_t starttime1,endtime1;
starttime1=clock();
//要计时的运行内容
//
endtime1=clock();
showruntime(starttime1,endtime1);
}
查看机器最大线程数
#include<thread>
#include<iostream>
using namespace std;
int main(){
cout << std::thread::hardware_concurrency() <<endl;
}
简单多线程
简单例子一
#include <iostream>
#include <pthread.h>
#include <thread>
#include <string>
#include <vector>
using namespace std;
//线程可以获取的全局变量
int maxThreadNumber=thread::hardware_concurrency();
vector<string> numlist;
//现成的启动函数
void *oneThread(void *var1){
int index1=*((int*)var1) ;
cout<< numlist[index1] <<endl;
//线程安全退出
pthread_exit(NULL);
}
int main(){
// 获取机器最大线程数
pthread_t threads[maxThreadNumber];
int threadIndex[maxThreadNumber];
//线程创建返回状态标记, 0 代表正常
int rc;
int i;
for(i=0;i<maxThreadNumber;i++){
numlist.push_back("线程"+to_string(i));
threadIndex[i]=i;
rc = pthread_create(&threads[i], NULL,
oneThread, (void *)&(threadIndex[i]));
if (rc){
cout << "Error:无法创建线程," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}
例子二 线程内获取 程序运行的总时间差
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <ctime>
#include <fstream>
using namespace std;
#define NUM_THREADS 8
void showruntime(clock_t startTime, clock_t endTime){
cout << "The run time is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
}
void *PrintHello(void *threadid)
{
clock_t starttime1,endtime1;
// 对传入的参数进行强制类型转换,由无类型指针变为整形数指针,然后再读取
starttime1 = *((clock_t*)threadid);
string bigtestdatafile="data/test_data.txt";
getStringFromFile(bigtestdatafile);
//输出线程结束 运行时间
endtime1=clock();
showruntime(starttime1,endtime1);
pthread_exit(NULL);
}
int main ()
{
clock_t starttime1,endtime1;
starttime1= clock();
pthread_t threads[NUM_THREADS];
int indexes[NUM_THREADS];// 用数组来保存i的值
int rc;
int i;
for( i=0; i < NUM_THREADS; i++ ){
cout << "main() : 创建线程, " << i << endl;
indexes[i] = i; //先保存i的值
// 传入的时候必须强制转换为void* 类型,即无类型指针
rc = pthread_create(&threads[i], NULL,
PrintHello, (void *)&(starttime1));
if (rc){
cout << "Error:无法创建线程," << rc << endl;
exit(-1);
}
}
endtime1=clock();
// showruntime(starttime1,endtime1);
pthread_exit(NULL);
}