在做题的时候需要读入字符串,但是又不想使用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. grep的正则匹配使用方式

    grep正则匹配的命令方式如下 $ grep 'pattern1\|pattern2' filename 或 $ grep -E 'pattern1|pattern2' file grep默认是区分大 ...

  2. MySQL 列定义的类型是varchar,已建立索引,查询时如果传入的是数字,则无法利用索引,查询特别慢。

    类型不对,导致无法充分利用索引. 比如:select * from table_name_xxx where name = "1234";  ----  查询很快 ,能够使用到na ...

  3. iOS数据持久化 - CoreData

    前言 1 - CoreData 是苹果公司封装的进行数据持久化的框架,首次在 iOS 3.0 版本系统中出现,它允许按照实体-属性-值模型组织数据,并以 XML.二进制文件或者 SQLite 数据文件 ...

  4. js 自定义可编辑table并获取输入值

    1.js加载table,tabid为abc. jsp: <table id="abc"></table> js:var tr_tr = "&quo ...

  5. java post格式发送application/x-www-form-urlencoded

    import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.*; ...

  6. 为什么要有jvm,jvm的作用?

    jvm的两个作用:第一.运行并管理java源码文件所生成的Class文件.第二.在不同的操作系统上安装不同的jvm,从而去实现跨平台的一个保障. 一般情况下,即使不熟悉jvm的运行机制,也不影响业务代 ...

  7. PGI 用户手册之 Site-Specific Customization of the Compilers

    翻译自PGI Compiler User's Guide 1.6. 使用PGI编译器进行并行编程 PGI编译器支持多种样式的并行编程: 使用pgf77,pgf95,pgfortran,pgcc或pgc ...

  8. js过滤掉指定html标签

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

  9. C++ 函数类型和函数指针类型的自动推导、声明和赋值

    1.函数类型推导 #include <iostream> bool MyComp(int val1, int val2) { return val1 > val2; } int ma ...

  10. iptables(二)常用规则即操作示例

    常用规则示例 修改chain默认策略 #filter表在INPUT chain默认策略为ACCEPT[root@iptables_host02 ~]# iptables -nvL INPUTChain ...