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码和数字直接加 ...
随机推荐
- [部署日记]Android Studio在同步Gradle的时候提示Could not resolve com.android.tools.build:gradle:7.0.4
一波未平一波又起,好家伙直呼好家伙. 问题来源: 在想好APP起什么名字,放哪里后,打开模拟器准备着手处理时发现, Gradle sync出问题了... "Build"窗口提示: ...
- svn 中如何checkout出单个文件
A 通过命令行操作 1.检出目录images svn co --depth=empty http://www.iusesvn.com/project1/images images_work_dir 这 ...
- nginx 日志分析之 access.log 格式详解
说明:access.log 的格式是可以自己自定义,输出的信息格式在nginx.conf中设置 一般默认配置如下: http { ... log_format main '$remote_addr - ...
- 手机安装APK文件,出现-应用未安装-软件包无效-安装包异常
在项目的根的gradle.properties文件中添加 android.injected.testOnly=false 即可. 猜想:因为是在打debug包,然后这个属性变为了true?然后手机会因 ...
- unity 利用相机截图,可以截取UI,保存png格式,可用于签名抠图
public Camera cam; void Start() { StartCoroutine(CaptureAlphaCamera(cam,new Rect(0,0,1920,1080))); } ...
- jmeter中监听器及测试结果分析
- vue-用户管理系统
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 执行celery --version报错
python 3.7.4安装celery后执行celery --version报错 安装命令: pip install celery -i https://pypi.douban.com/simple ...
- <<运算?&=、|=、 ^=、<<=、>>=的意思? 十六进制前缀是 0x。
<<运算? a<<b 表示把a转为二进制后左移b位(在后面添加 b个0).例如100的二进制表示为1100100,100左移2位后(后面加2个零):1100100<< ...
- kotlin channel使用注意点
kotlinx.coroutines.channels.ClosedSendChannelException: Channel was closed at kotlinx.coroutines.cha ...