在做题的时候需要读入字符串,但是又不想使用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. vscore 中 vim 常用快捷键

    谷歌浏览器 ctrl + T 新建一个页面 ctrl + J 查看下载界面 F6 直接搜索 vscore 在 vscore 中使用 vim 建议去掉 ctrl 键的功能捆绑,不然会覆盖掉很多的 vsc ...

  2. usb 2.0 packet

    注意PID[7:0] = {~pid[3:0], pid[3:0]}

  3. C# DataGrid嵌套DataGrid动态隐藏显示行

    前端代码: <Window x:Class="DataGridPractice.MainWindow" xmlns="http://schemas.microsof ...

  4. JVM系列(三):JVM内存结构和参数说明

    一.概述,内存结构图 二.堆Heap,存放对象实例,是垃圾回收的主要区域,非堆的内存不进行GC,GC会导致程序运行中断, 物理上可以不连续,堆空间不足时会产生OutOfMemoryException, ...

  5. Excel 的盒须图 离群值 Outliers

    Excel 中的盒须图 翻译自https://www.excel-easy.com/examples/box-whisker-plot.html 本示例教您如何在Excel中创建盒须图.盒须图显示了数 ...

  6. js过滤掉指定html标签

    替换标签 var str = "<p><span style='color:#ccc;'>这是测试标签</span><span>这是测试htm ...

  7. Java常用数据结构

    1.数组 数组(Array) 是一种很常见的数据结构.它由相同类型的元素(element)组成,并且是使用一块连续的内存来存储. 我们直接可以利用元素的索引(index)可以计算出该元素对应的存储地址 ...

  8. https原理(三)双向实践(curl)

    接 https代理服务器(三)实践,实践双向ssl 本文采用客户端与服务端不同密钥对 1 mkcert myclient 生成客户端公钥 私钥 2 mkcert -pkcs12 myclient 也可 ...

  9. logstash输出到MySQL

    1.安装插件/bin/logstash-plugin install logstash-output-jdbc 2.下载jdbc  https://mvnrepository.com/artifact ...

  10. 制作docker php5.6的镜像

    docker pull centos Docker run -it --name="centos-test" centos yum install wget  # 报错在后面有处理 ...