LeetCode中题目中常见的一些数据输入函数

这里是公共头文件。

#include<stdio.h>
#include<iostream>
using namespace std;
#include <vector>

接收用户输入的字符串

//接收用户输入字符串,并倒序输出
#include <stdio.h>
#include <string.h>
//gets方法
int main(void)
{
    char str1[1000];    
    char str2[1000];
    gets(str1);//在C11标准中被删除
    int num = strlen(str1);
    for(int i = 0; i < num; i++)
        str2[i] = str1[num - 1 - i];
    str2[num] = 0; 
    printf("%s",str2);
    return 0;
}

//cin.getline方法
#include <iostream>
using namespace std;
int main()
{
	char m[20];
	cin.getline(m, 20); //与上面基本相同。
	cout << m << endl;
	return 0;
}

//cin.get(字符数组名,接收字符数)用来接收一行字符串,可以接收空格
#include <iostream>
using namespace std;
void main ()
{
	char a[20];
	cin.get(a,20); //有些类似getline。可以输入多个单词,中间空格隔开。
	cout<<a<<endl;
}

//cin >> string方式
//举例 《写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入)》
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
    string str;
    while (cin >> str)
    {
        int len = str.size();
        int sum = 0;
        //十六进制转换为十进制
        for (int i = len - 1; i >= 0; --i)
        {
            // 数字字符的转换,ASCII码:'0'——>48,十六进制:0——>0
            if (str[i] >= '0' && str[i] <= '9')
            {
                sum += (str[i] - '0') * pow(16, len - 1 - i);
            }
            // 字母字符的转换,ASCII码:A——>65,十六进制:A——>10
            else if (str[i] >= 'A' && str[i] <= 'F')
            {
                sum += (str[i] - 'A'+10) * pow(16, len - 1 - i);
            }
        }
        cout << sum << endl;
    }
}

删除字符串首尾空格

//删除字符串首尾空格
void trim(string &s)
{
	if (!s.empty())
	{
		s.erase(0, s.find_first_not_of(" "));
		s.erase(s.find_last_not_of(" ") + 1);
	}
}

分割字符串

//分割字符串1
vector<string> split(const string& str, const string& pattern)
{
	vector<string> ret;
	if (pattern.empty()) return ret;
	size_t start = 0, index = str.find_first_of(pattern, 0);
	while (index != str.npos)
	{
		if (start != index)
			ret.push_back(str.substr(start, index - start));
		start = index + 1;
		index = str.find_first_of(pattern, start);
	}
	if (!str.substr(start).empty())
		ret.push_back(str.substr(start));
	return ret;
}

//分割字符串2
vector<string> split(const string &str, const string &pattern)
{
	//const char* convert to char*
	char * strc = new char[strlen(str.c_str()) + 1];
	strcpy(strc, str.c_str());
	vector<string> resultVec;
	char* tmpStr = strtok(strc, pattern.c_str());
	while (tmpStr != NULL)
	{
		resultVec.push_back(string(tmpStr));
		tmpStr = strtok(NULL, pattern.c_str());
	}

	delete[] strc;

	return resultVec;
}

分割字符串并删除首尾空格

//分割字符串并删除首尾空格

vector<string> splitAndTrim(const string& str, const string& pattern)
{
	vector<string> ret;
	if (pattern.empty()) return ret;
	size_t start = 0, index = str.find_first_of(pattern, 0);
	string tmp;
	while (index != str.npos)
	{
		if (start != index)
		{
			tmp = str.substr(start, index - start);
			trim(tmp);
			ret.push_back(tmp);
		}
			
		start = index + 1;
		index = str.find_first_of(pattern, start);
	}
	if (!str.substr(start).empty())
	{
		tmp = str.substr(start);
		trim(tmp);
		ret.push_back(tmp);
	}

	return ret;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

thequitesunshine007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值