C/C++算法编程基础篇:常见数据类型、输入与输出


前言

这个栏目是对我算法学习过程的同步记录,我也希望能够通过这个专栏加深自己对编程的理解以及帮助到更多像我一样想从零学习算法并参加竞赛的同学。在这个专栏的文章中我会结合在编程过程中遇到的各种问题并提出相应的解决方案。当然,如果屏幕前的你有更好的想法或者发现的错误也欢迎交流和指出!不喜勿喷!不喜勿喷!不喜勿喷!那么事不宜迟,我们马上开始吧!


一、常见数据类型

对于算法中涉及的常见的数据类型,目前主要有以下几种:

(1)整型(int):还可根据所占内存空间大小和符号正负进一步细分。

具体可写为:signed int、unsigned int、long int、unsigned long int、long long(超长整型)

这个对于学习单片机的同学想必并不陌生,在采集数据时我们会经常使用如uint16_t等数据类型。

(2)实型:单精度型(float)、双精度型(double),我们一般只用double类型。

(3)字符型(char):用单引号,如char a = 'a';进行赋值操作。

(4)数组:可诸如 int array[5] = {0};进行初始化,则此数组中有5个元素,目前全部初始化为0

(5)字符串:一种特殊的数组,元素是字符。可进行如下声明和初始化:char a[5]; 或string a = "hello";(需包括<string.h>头文件或者万能头文件<bits/stdc++.h>)

目前我们基本上只会接触这几种数据类型,有新的后面再说。

关于字符串的更多操作和性质会在之后的文章进行阐述。


二、配置的常见问题和解决方案

1.关于万能头文件<bits/stdc++.h>

注意,Visual Studio 并不支持万能头文件的使用,如果有需要,需自行配置。具体步骤如下:

1) 在桌面新建一个文件名为bits的文件夹

2) 在bits文件夹中新建一个名为stdc++.h的头文件

3) 将下面的代码复制进去

// C++ includes used for precompiling -*- C++ -*-
 
// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
 
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
 
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <Licenses - GNU Project - Free Software Foundation>.
 
/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */
 
// 17.4.1.2 Headers
 
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
 
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
 
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
 
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

将文件夹用管理员权限移动到Visual Studio IDE\VC\Tools\MSVC\14.36.32532\include\bits即可。路径可能略有不同,根据实际情况来。使用这个头文件可以减少竞赛时花费的时间,但同时也会增加编译的时间。要注意的是,这个头文件不利于代码的移植,因此建议只在竞赛中使用。

2.关于【error C4996: 'scanf': This function or variable may be unsafe】

可以在源文件最前面加上如下的预定义语句。

#define _CRT_SECURE_NO_WARNINGS

三、C的输入与输出

1.scanf()与printf()

(1) 格式输入函数scanf

语法:scanf(<格式控制字符串>, <变量地址列表>);

例如:

int a;
scanf("%d", &a);
double b;
scanf("%lf", &b);

下面给出了一些常用的数据类型及其标识符: 

数据类型格式转换说明符用法
int%d
double%lf
char%c空白字符(空格、回车、制表符)也作为有效输入
char[]%s

关于域宽修饰符的用法如下:

int a, b, c;
scanf("%2d%3d%4d", &a, &b, &c);//1234567890
//此时的输入结果: a=12 b=345 c=6789 最后一位的0被忽略

(2) 格式输出函数printf

语法:printf(<格式控制字符串>, <输出参数表>);

对于输出函数,可以自行选择输出时的精度:

double pi = 3.14159;
printf("%.3f", pi); //3.141
//包括单双精度,输出时的格式标识符都是%f,这不同于scanf

(3) 综合使用(以对字符串的操作为例,中间会穿插字符串的一些知识)

例1:

char charArray[5];
scanf("%s", charArray);//Hello World
printf("%s", charArray);//Hello
//注意:%s遇到空格或回车就会停下
//这里charArray前面不要加取址符是因为其本身就是指针,指向首元素

对于字符串charArray来说,我们可以进行如下拆解:

Hello\0??

当读取到空格时,这一位在字符串中用\0这一特殊符号表示,代表结束,后面的内容读取不到。

例2:排除符号的使用

char charArray[15];
scanf("%[^\n]", charArray); //排除符号,这里表示接受除回车外的所有输入
                            //[] 中的是正则表达式
printf("%s", charArray);

例3:关于string的输入输出

string str;
str.resize(5);          //预先分配空间
scanf("%s", &str[0]);   //hello
cout << str;            //hello

小总结:printf和scanf主要有格式化输入输出和效率高两大优点。

四、C++的输入输出流

1.取消同步流

cin和cout由于自身要进行数据类型判断等原因,效率较低。那么此时我们就需要取消同步流,防止数据量大时造成程序运行超时。下面是取消同步流的代码:

ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

2.cin与cout综合运用

废话不多少,我们直接通过几个代码案例加深理解吧!

例1:自定义精度的输出

double a,b;
cin >> a >> b;
cout << fixed << setprecision(3) << a << ' ' << b << endl;//保留小数点后3位

例2:cin输入字符串也是遇到空格和回车就结束

char a[10];
cin >> a;         //Hello World
cout << a << endl;//Hello

要想获得完整的输入,我们可以使用getline()

string a;
getline(cin, a);   //hello world
cout << a << endl; //hello world

例4:字符串综合输入输出

#include<bits/stdc++>
using namespace std;

const int N = 150;
char s[N];

int main()
{
    cin >> s + 1; //从s[1]开始输入  //
    for(int i = 1; s[i]; ++i) cout << s[i]; //循环条件 s[i] != '\0'
    return 0;
}

 


总结

我也从没想过输入输出还有这么多的花样。总而言之,如果你觉得这篇文章还不错,劳烦多多支持一下!感谢你的观看!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值