鱼C论坛

 找回密码
 立即注册
查看: 1565|回复: 3

[已解决]cin输入数组类型的问题

[复制链接]
发表于 2022-11-25 07:58:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码
#include<iostream>
using namespace std;


int main()
{
       

        char ch[10];
        cin >> ch;
        cin >> ch + 1;
        cout << ch[0] << endl;
        cout << ch[1] << endl;


        /*int INT[10];
        cin >> INT + 1;
        */
       


        return 0;
}
看到了出入char型数组的时候可以不用加[],而int型数组却不可以,是为什么呢
最佳答案
2022-11-25 10:20:50
是这个函数,库里面有这个函数,C++20引入的
参考:https://zh.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2
  1. template <class CharT, class Traits, std::size_t N>
  2. basic_istream<CharT, Traits> &operator>>(basic_istream<CharT, Traits> &st, CharT (&s)[N]);
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-11-25 10:06:13 | 显示全部楼层
因为库里面没有这样的函数
  1. template <size_t num>
  2. istream &operator>>(istream &is, int (&a)[num]);
复制代码

  1. #include <iostream>

  2. using namespace std;

  3. template <size_t num>
  4. istream &operator>>(istream &is, int (&a)[num]) {
  5.     for(auto &i: a) {
  6.         if(!(is >> i)) break;
  7.     }
  8.     return is;
  9. }

  10. int main() {
  11.     char ch[10];
  12.     cin >> ch;
  13.     int a[10];
  14.     cin >> a;
  15.     return 0;
  16. }
复制代码

  1. cin >> ch;
复制代码

这个能用是因为库里面有像下面这样的函数
  1. template <size_t num>
  2. istream &operator>>(istream &is, char (&a)[num]);
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-25 10:20:50 | 显示全部楼层    本楼为最佳答案   
是这个函数,库里面有这个函数,C++20引入的
参考:https://zh.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2
  1. template <class CharT, class Traits, std::size_t N>
  2. basic_istream<CharT, Traits> &operator>>(basic_istream<CharT, Traits> &st, CharT (&s)[N]);
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 13:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表