scanf 读入 string 注意点
在做题的时候需要读入字符串,但是又不想使用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 注意点的更多相关文章
- c语言中怎样用scanf()读入带空格的字符串?
楼主 发表于: 2011-01-14 15:39:55 #include <stdio.h> int main(void){ int i; char a[5]; scanf("% ...
- string用scanf读入printf输出(节省时间)
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...
- string用scanf读入(节省时间)
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...
- 如何用scanf读入一个string
#include <stdio.h> #include <string> using namespace std; int main() { string a; a.resiz ...
- string字符串类型用scanf读入,printf输出
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...
- 原来scanf读入字符串还能这样..
(本文针对于NOIP Day1 玩具迷题) (这是弱鸡写的)(字符串用char二维,本质一样的) 在NOIP成功AC了这道题,结果OJ上被string卡了时间,没办法只能用scanf了.....百度看 ...
- scanf读入与printf输出
作为一个资深$cin,cout$玩家,在多次因为$cin$太慢被吊打后,开始反思有必要认真地学一下$scanf$和$printf$了$\cdot \cdot \cdot$ 格式 $scanf( &qu ...
- 将整个文件读入string中
size_t GetFileSize(FILE* file) { fseek(file, , SEEK_END); return static_cast<size_t>(ftell(fil ...
- scanf读入有空格字符串
当不支持gets时,getline又比较慢,可以使用scarf("%[^\n]s", str);来读入以换行表示读完的字符串,其中[^char]表示以char为结束.
- C++中数字与字符串之间的转换 scanf string总结(复习必读)
1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...
随机推荐
- java使用apache-commons-lang3生成随机字符串(可自定义规则)
在日常开发中,我们经常会遇到生成随机字符串的需求.可能是大小写字母+数字,也可能是其他各种字符.作为一个常用功能,我们完全没必要自己实现,有很多优质的类库已经做的很完善了.本文介绍的就是apache- ...
- BAPI_GOODSMVT_CREATE - 101 mvt. Message ERROR M7427
Message SAP M7427 - Entered batch &1 does not match batch &2 in the purchase order 639934 - ...
- jsonpath表达式
JsonPath是一种简单的方法来提取给定JSON文档的部分内容,其中正则表达式的包含或不包含配制有时候非常有用! json操作实例 { "store": { "book ...
- shell—if + case条件语句
if 条件语句 1. 概述 在shell的各种条件结构和流程控制结构中都要进行各种测试,然后根据测试结果执行不同的操作,有时候也会与 if 等条件语句相结合,来完成测试判断,以减少程序运行错误. 2. ...
- iOS 过滤字符串
//表示去掉字符串中的/符号 sysdate:[[self Gettime:strbegindate] stringByReplacingOccurrencesOfString:@"/&qu ...
- 总结ref和out的区别
之前每次遇到ref和out时,老是忘记他们的使用方法和区别.每次都要网上搜一下别人写的博客来回忆.这次干脆自己整合一下别人博客的内容,方便下次忘记时查询. 用途: 在C#中通过使用方法来获取返回值时, ...
- Android移动端性能测试工具mobileperf
简介:mobileperf是阿里开源的一个python PC 工具,可以收集Android性能数据: cpu.内存.流畅度.fps.logcat日志.流量.进程线程数.进程启动日志,mobileper ...
- clear_buff-cache.sh
#! /bin/bash sync;echo 1 > /proc/sys/vm/drop_caches # 表示清除pagecache sync;echo 2 > /proc/sys/vm ...
- E: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用)E: 无法对目录 /var/lib/apt/lists/ 加锁
问题: 解决:https://www.cnblogs.com/long5683/p/11058066.html 使用方法二 可以
- ComPiler200003:Story-Oriented Programming
Story-Oriented Programming MAY 25TH, 2018 http://www.brandonkeown.com/2018/05/story-oriented-program ...