在做题的时候需要读入字符串,但是又不想使用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. 黑马程序员Javaweb综合案例错误总结整理

    案例整理(呕心沥血的教训) 其他的我大部分还是不知道那里出了问了,我这个新建的项目must3终于成功了 那个品牌名称和企业名称没有,是要在BrandMapper里加注解@ResultMap 一直报那个 ...

  2. Java基础——IO基础知识

    字节流可以处理任何类型的数据(图片.MP3.视频等文件),字符流只能处理字符类型(文本文件)的数据.

  3. The Nine Arches Bridge

  4. loadrunner---脚本录制常见问题

    一:loadrunner录制脚本时ie浏览器不弹出? 1.IE浏览器取消勾选[启用第三方浏览器扩展]启动IE,从[工具]进入[Internet选项],切到高级,去掉[启用第三方浏览器扩展(需要重启动) ...

  5. SpringBoot_Thymeleaf项目开发

    用Springboot集成Thymeleaf,开发一个前后端不分离的Web项目,记录下每个步骤:(IDEA版) 一.项目初始化: 1.打开idea,以次点击 File -- New -- Projec ...

  6. 攻防世界-fileinclude

    一道简单的文件包含题目,源代码如下 一.代码分析 此题中关键代码为 分析此处代码可知,$lan的值是cookie中language所对应的值,当该值不为english时,会将$lan的值与.php字符 ...

  7. oracle之如何获取执行计划方法

    1.什么是执行计划 为了执行sql语句,Oracle在内部必须实现许多步骤,这些步骤可能是从数据库中物理检索数据行,或者用某种方法来准备数据行等,接着Oracle会按照一定的顺序一次执行这些步骤,最后 ...

  8. PyQt5弹框定时关闭(python)

    PyQt5使用QMessageBox,可以设置在几秒后关闭  (作者使用的python3) info_box = QMessageBox()# 因为没使用这种方式 QMessageBox.inform ...

  9. 互联网公司IT系统架构进化之路

    一日,与一高手在茶馆聊天.他问道:在鞋厂剑派这两年,可习的什么高深的剑法?我不由一愣,细细想来,这两年每日练习的都是简单的劈砍动作和一些简练的套路.并没有去练什么高深的剑法.不过鞋厂剑派在江湖上也算小 ...

  10. sap IUT240 Contract Accounts Receivable and Payable pdf

    sap IUT240 Contract Accounts Receivable and Payable  pdf sap IUT240 Contract Accounts Receivable and ...