在做题的时候需要读入字符串,但是又不想使用char 数组,于是采用string存储,当时遇到了scanf读取string失败,查阅资料后总结下。

scanf是c的标准输入输出流,想要读入string,需要提前对string分配足够大的空间,否则会截断数据,其次scanf的参数需要string[0]。

test 1: read a signle string using scanf
#include <bits/stdc++.h>
using namespace std;
int main()
{
string word;
word.resize(100); // 提前分配好空间,更建议使用cin
scanf("%s", &word[[0]);
cout << word << endl;
return 0;
}
test 2: read a vector string using scanf
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<string> words;
string word;
word.resize(100);
for(int i = 0; i < 3; i++)
{
scanf("%s", &word[0]);
words.push_back(word);
}
return 0;
}
补充提高cin速度,一行代码搞定:

std::ios::sync_with_stdio(false);

scanf 读入 string 注意点的更多相关文章

  1. c语言中怎样用scanf()读入带空格的字符串?

    楼主 发表于: 2011-01-14 15:39:55 #include <stdio.h> int main(void){ int i; char a[5]; scanf("% ...

  2. string用scanf读入printf输出(节省时间)

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

  3. string用scanf读入(节省时间)

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

  4. 如何用scanf读入一个string

    #include <stdio.h> #include <string> using namespace std; int main() { string a; a.resiz ...

  5. string字符串类型用scanf读入,printf输出

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

  6. 原来scanf读入字符串还能这样..

    (本文针对于NOIP Day1 玩具迷题) (这是弱鸡写的)(字符串用char二维,本质一样的) 在NOIP成功AC了这道题,结果OJ上被string卡了时间,没办法只能用scanf了.....百度看 ...

  7. scanf读入与printf输出

    作为一个资深$cin,cout$玩家,在多次因为$cin$太慢被吊打后,开始反思有必要认真地学一下$scanf$和$printf$了$\cdot \cdot \cdot$ 格式 $scanf( &qu ...

  8. 将整个文件读入string中

    size_t GetFileSize(FILE* file) { fseek(file, , SEEK_END); return static_cast<size_t>(ftell(fil ...

  9. scanf读入有空格字符串

    当不支持gets时,getline又比较慢,可以使用scarf("%[^\n]s", str);来读入以换行表示读完的字符串,其中[^char]表示以char为结束.

  10. C++中数字与字符串之间的转换 scanf string总结(复习必读)

    1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...

随机推荐

  1. linux 安装简洁的 zsh

    为什么要安装简洁的 zsh zsh 是 shell 中的佼佼者,但是网上配置 zsh的方案,千篇一律的都是配置的 oh-my-zsh,个人感觉非常臃肿,配置低的话,用起来还会非常卡. 安装 zsh 本 ...

  2. 2023Hgame

    2023Hgame Shared Diary 源代码先放一下 const express = require('express'); const bodyParser = require('body- ...

  3. 1,docker遇到的问题1

    pushd "%~dp0"dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txtfor /f %% ...

  4. easyExcel-modle

    package com.alibaba.easyexcel.test.model;import com.alibaba.excel.annotation.ExcelProperty;import co ...

  5. Mybatis开发之mapper代理实现自定义接口(常用)

    Mybatis开发之mapper代理实现自定义接口(常用) 通过mapper代理实现自定义接口 自定义接口,接口里面定义定义相关的业务方法 编写方法相对应的Mapper.xml. 定义完接口后,Map ...

  6. mysql慢sql监控

    1.思路 之前用 mysql 一直没有考虑到这点,mysql 慢 sql 监控是很重要的,它能帮我们梳理我们的业务 sql 到底是哪里处了问题,那么慢 sql 监控怎么做呢? 有两种思路来实现: 在应 ...

  7. wait notify 实例,生产消费者模式(转)

    今天发现了一段很标准的多线程代码,记得以前也写过,但是没有这个这么小巧和标准. import java.util.LinkedList; import java.util.Queue; import ...

  8. mysql添加到环境变量

    今天换新系统,以前的一些常用软件重新安装了一下,安装到mysql我还是按照以前的习惯选择了低版本的5.7系列,突然想要装一把,像python一样可以直接访问解释器,能不能直接在cmd中输入mysql就 ...

  9. How to Fix SSH Failed Permission Denied

    https://phoenixnap.com/kb/ssh-permission-denied-publickey

  10. Flink Application Development DataStream API Event Time--Flink应用开发DataStream API事件时间

    目录 概览 事件时间 接下来去哪儿 水印生成 水印策略简介 使用水印策略 处理空闲源 写水印生成代码 写周期WatermarkGenerator代码 写符号形式的WatermarkGenerator代 ...