C++学习

C++可以做的事
https://github.com/fffaraz/awesome-cpp#game-engine
市场上c++用来做什么
https://www.zhihu.com/question/304959838
C++库函数
https://zhuanlan.zhihu.com/p/98056565
c++ github项目
https://www.zhihu.com/question/67158058
https://www.cnblogs.com/chulia20002001/p/6956678.html
c++学习网站
https://www.cnblogs.com/cwq2014/p/4210984.html
https://www.freecplus.net/index.html
https://www.runoob.com/cplusplus/cpp-tutorial.html
https://blog.csdn.net/qq_36941368/article/details/108427519

string使用方法及用途
一、截取多个类后面的.字符段如A.B.C.m_szname,需要获取m_szname

实现:str2=str1.substr(str1.rfind('.') == std::string::npos ? 0, str1.rfind('.')+1);

二、此代码为找字串b,字串c存在的交集,并把交集赋给a。

int srand(string& a, const char* b,const char* c) {
	a = "";
	stringstream ss;
	int i;
	for(i = 0; i < strlen(c); i++){ 
		if (strchr(b, c[i])) {
			ss << c[i];
		}
	}
	a = ss.str();
	ss.clear();
	return 0;
}

1. atoi用法

   atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数。
   例子:char *str = "12345.67";    n = atoi(str);
输出为12345

2. string:npos使用
查找字符串a是否包含子串b,不是用strA.find(strB) > 0 而是 strA.find(strB) != string:npos
其中string:npos是个特殊值,说明查找没有匹配
string a = “账号错误,重新登录”; if (a.find(“账号错误”)!= string::npos) { cout << “错了”; }
输出为:错了。————表明包含该字符。

#include<iostream>
using namespace std;
#include <string>
#include <stdio.h>
int main(void)
{
	//strncpy_s,strchr,strncpy代码实践
	char* str1 = "abc|opq";
	char* str2 = strchr(str1,'|');//此处返回的是一个地址,不是索引。
	int len = str2 - str1;//此处返回|前端的差值
	string aa = str2;
	cout << len << endl<<aa<<endl;
	char str3[6], str4[10];
	strcpy_s(str4,sizeof(str4),str1);
	cout <<str4 << endl;
	strncpy_s(str3,sizeof(str3),str1,len);
	string dd = str3;
	cout << dd << endl << str3[len] << endl;
	str3[len] =0;
	cout <<str3 <<str3[len]<< endl;
	return 0;
}

实验结果
3. C++中string的find()函数的用法

string的find()函数用于找出字母在字符串中的位置。
	find(str,position)
	find()的两个参数:str:是要找的元素,position:字符串中的某个位置,表示从从这个位置开始的字符串中找指定元素。可以不填第二个参数,默认从字符串的开头进行查找。
	返回值为目标字符的位置,当没有找到目标字符时返回npos。

4. replace() 方法参考:https://blog.csdn.net/cai_niaocainiao/article/details/81260902

str.replace(position,length,"asc");position为起始位置,length为连续的长度,asc为要替换的值。

5. append应用

string aa ("select ");
aa.append("* ").append("from ").append("table;");
输出为:select * from table;

6. string_toLower使用

bool string_toLower(str);	把str转为小写;注意返回值为bool型,不能用str去接收。

7. 头文件sstream及strncpy_s使用:

#include<sstream>
std::stringstream ss;	
std::string error_str;			
ss << "文件" << _“123<< ":捕获异常";												
ss >> error_str;	
输出为文件123捕获异常
// strncpy_s.cpp
//
#include "stdafx.h"
#include <iostream>
#include <cstring>
int main()
{
    using namespace std;
 
    char str1[5] = "abcd";
    char str2[10] = "leonardo";
    cout << "str1 is " << endl << str1 << endl;
    cout << "str2 is " << endl << str2 << endl;
    strncpy_s(str2, sizeof(str2), str1, 3);
    cout << "after the copy str2 is " << endl << str2 << endl;
 
    system("pause");
    return 0;
}

在这里插入图片描述

8. C语言宏定义和宏定义函数
https://blog.csdn.net/ylgrgyq/article/details/6822440?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
看下面的例子:

#define MALLOC(n, type) \
  ( (type *) malloc((n)* sizeof(type)))

利用这个宏,我们就可以为任何类型分配一段我们指定的空间大小,并返回指向这段空间的指针。我们可以观察一下这个宏确切的工作过程:

  int *ptr;
  ptr = MALLOC ( 5, int );
  将这宏展开以后的结果:
  ptr = (int *) malloc ( (5) * sizeof(int) );

char* + int 的加法
"testchar" + 1 = "estchar"
"testchar" + '9' = ""
其实这个时候只是char*的指针与后面的值进行相加,看来C++里要时刻注意指针。

9. c++瞎操作

#include <iostream>
#include <string>
#define xxx  "---胡歌---"
using namespace std;
int main() {
	int ca = 1111;
	char dd[20] = { 0 };
	sprintf_s(dd, sizeof(dd), "%d", ca);
	string a = "string123"\
		"你是""--------" + string(dd) +		//把变量加入字符串
		"" xxx"哒哒"							//宏定义加入字符
		"\nhuge\n";
	cout << a;
	return 0;
}

运行结果·
10. substr方法

#include<string>
#include<iostream>
using namespace std;
int main()
{
    string x="Hello_World";
    /*默认截取从0到npos.重载原型为string substr(_off=0,_count=npos);npos一般表示为string类中不存在的位置,_off表示字符串的开始位置,_count截取的字符的数目*/
    cout<<x.substr()<<endl;
    cout<<x.substr(5)<<endl;//截取x[5]到结尾,即npos.重载原型为string substr(_off,_count=npos)
    cout<<x.substr(0,5)<<endl;//以x[0]为始,向后截取5位(包含x[0]),重载原型string substr(_off,_count)
    /*
    备注:
    指定的截取长度加起始位置即_off+_count>源字符串的长度,则子字符串将延续到源字符串的结尾
    */
}

11. find与rfind方法
string中的find函数与rfind函数定义如下:

int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置

int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值 
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
在实际的程序实现中,rfind的查找截止值并不是pos,而是pos+strlen( c )-1

12. find_first_not_of()和find_last_not_of()函数介绍

find_first_not_of()函数:
  正向查找在原字符串中第一个与指定字符串(或字符)中的任一字符都不匹配的字符,返回它的位置。若查找失败,则返回npos(npos定义为保证大于任何有效下
标的值。)
find_last_not_of()函数:	
  正向查找在原字符串中最后一个与指定字符串(或字符)中的任一字符都不匹配的字符,返回它的位置。若查找失败,则返回npos。
(npos定义为保证大于任何有效下标的值。)

13. erase用法

// string::erase
#include <iostream>
#include <string>
#include<sstream>
using namespace std;

int srand(string& a, const char* b, const char* c) {
	a = "";
	stringstream ss;
	int i;
	for (i = 0; i < strlen(c); i++) {
		if (strchr(b, c[i])) {
			ss << c[i];
		}

	}
	a = ss.str();
	ss.clear();
	return 0;
}

int main()
{
	std::string str("This is an example sentence.");
	std::cout << str << '\n';
	// "This is an example sentence."
	str.erase(10, 8);                        
	std::cout << str << '\n';
	// "This is an sentence."
	str.erase(str.begin() + 9);             
	std::cout << str << '\n';
	// "This is a sentence."
	str.erase(str.begin() + 5, str.end() - 9);  
	std::cout << str << '\n';
	// "This sentence."

	string d = "";
	char * b = "胡是歌对的";
	char * c = "胡歌歌是吧";
	srand(d, b, c);
	cout << d << endl;

	string str3("Test string");
	for (std::string::iterator it = str3.begin(); it != str3.end(); ++it)
		cout << *it << &it;
	cout << '\n';
	system("pause");
	return 0;
}

结果

14. 字符串切割函数

#include<stdio.h>
#include<string.h>
int main(){
    char input[16]="abc,d,ef,g";//待分解字符串
    char* delim=",";//分隔符字符串
    char* p=strtok(input,delim);//第一次调用strtok
    while(p!=NULL){//当返回值不为NULL时,继续循环
        printf("%s\n",p);//输出分解的字符串
        p=strtok(NULL,delim);//继续调用strtok,分解剩下的字符串
    }
    return 0;
}

锁的由来和部分代码
https://blog.csdn.net/bian_qing_quan11/article/details/73734157?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

https://www.cnblogs.com/fenghualong/p/11568398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值