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码和数字直接加 ...
随机推荐
- 使用JSONObject将实体类,String类型和JSON类型相互转换(java)
使用JSONObject将实体类,String类型和JSON类型相互转换(java) https://blog.csdn.net/weixin_42424720/article/details/846 ...
- MyBatis_07(动态SQL)
动态SQL: Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能, 它存在的意义是:"为了解决拼接SQL语句字符串时的痛点问题". 一.If if标签可通 ...
- 学习-Vue3-条件渲染
v-if支持在 <template> 元素上使用,能和 v-else 搭配使用. v-show 不支持在 <template> 元素上使用, 也不能和 v-else 搭配使用. ...
- 修改 Ubuntu 的软件源
1.将 /etc/apt/ 路径下的 sources.list 的内容修改为如下内容(此内容为 Ubuntu Kylin 里面的内容,直接拿过来用,也可以用其它的国内的源). deb http://m ...
- 使用myBadboy(python自主开发工具)启动谷歌浏览器并自动录制jmeter脚本
一.源代码下载 https://gitee.com/rmtic/mybadboy 说明:因现有的录制方法有不能定制等不足之处,所以自力更生,自动生成对应jmeter脚本,减少维护成本 二.操作说明 1 ...
- Excel error - the macros in this project are disabled, please refer to the online help or documentation of the host application to determine how to enable macros.
alt+F11 进入vba界面,F5运行macro后报错. Sub 合并当前工作簿下的所有工作表() Application.ScreenUpdating = False For j = 1 To S ...
- Google big query - 怎么创建临时表/create temp table
Creating a temporary table The following example creates a temporary table named Example and inserts ...
- 解决vscode中,powershell中conda activate无效--更改vscode默认的shell为anaconda shell
问题记录: windows系统里,cmd可以正常使用conda activate 命令,但是在powershell中,使用conda activate既不报错(说明路径没问题),也没激活conda环境 ...
- Zookeeper ZAB协议
这篇博客是从源码的角度了解Zookeeper 从接收客户端请求开始,到返回数据为止,有很多涉及到的对象创建因为在前几篇文章已经说明过了,这里就不再重复的说明了,如果不是很明白的的,可以先看前几篇博文了 ...
- mybatis-关联查询1-一对多关联查询
或者多表单独查询方式