std::cin

Input: Executing std::cin >> v discards any whitespace characters in the standard input stream, then reads from the standard input into variable v. It returns std::cin, which has type istream, in order to allow chained input operations.

int _tmain(int argc, _TCHAR* argv[])
{
        // ask for the person‘s name
        std::cout << "Please enter your first name: ";

        // read the name
        std::string name;         // define name
        std::cin >> name;         // read into

        // write a greeting
        std::cout << "Hello, " << name << "!" << std::endl;

    std::string stars(100, ‘*‘);      // construct the variable
    std::cout << stars;
    return 0;

}

Output:

std::cin

时间: 2024-07-30 16:06:09

std::cin的相关文章

while (std::cin &gt;&gt; value) 的结束条件

刚才无意从c++primer中发现的,就是Windows系统中一Ctrl+Z结束,而UNIX和macos则以Ctrl+D结束,不需要再输入一个其他字符结束. /*测试插入图片*/

std::ios::sync_with_stdio和tie()——给cin加速

平时在Leetcode上刷题的时候,总能看到有一些题中最快的代码都有这样一段 static const auto init = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return nullptr; }(); 有时候偏偏算法是一样的,但是速度要比没有这段代码的快很多: 查了一下这段代码其实是给cin加速的,也就是说上面提到的题应该是碰到的大数据的输入,而cin cout要比scanf  printf慢很多,很

C++ 关于大多数人将 cin::sync() 视为清除缓存区函数的错误

百度cin::sync(),得到的绝大多数解释都是用作清除缓冲区,并声明一般与cin::clear()函数一起用达到目的. 同样百度清除缓冲区的方法,也是绝大多数说用cin::sync()达到此目的. 然而cin::sync()却并非是用作清除缓冲区的,所以这样用有时候不能达到我们想要的清空缓冲流的效果. http://www.cplusplus.com/reference/istream/istream/sync/ 对于cin::sync,作用根据上述C++文档说明,应该为: Synchron

关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流

原文地址:http://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.html http://www.clanfei.com/2012/03/235.html 在网上查看别人的ACM代码时,发现别人输入输出语句用的总是scanf与printf,有点不解,还以为他们用的都是C语言,而非C++,但今天做的一道题(Sort): 发现与网上的其他高手使用完全相同的方法,使用sca

STL C++ std::bind操作例子,仿函数操作配合算法库操作

1.stl::bind 和std::mem_fun_ref系列的配合使用出现了问题,多参形式不知道如何组织.适配器的操作真心难受!!!只能迷迷糊糊地用着.要使用非质变算法时需要作用于容器时只能考虑lambda或者transfer操作.待续 // functor-adapter_p431.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <algorithm>//元素操作算法 #include <functiona

C++ cin输入流 详细用法

我们来看下下面这段代码: #include <iostream> #include <vector> #include <cstdlib> int main() { int num = 0; std::vector<int> ivec; do { std::cout << "please input some numbers:" << std::endl; while (std::cin >> num)

ACM比赛中如何加速c++的输入输出?如何使cin速度与scanf速度相当?什么是最快的输入输出方法?

在竞赛中,遇到大数据时,往往读文件成了程序运行速度的瓶颈,需要更快的读取方式.相信几乎所有的C++学习者都在cin机器缓慢的速度上栽过跟头,于是从此以后发誓不用cin读数据.还有人说Pascal的read语句的速度是C/C++中scanf比不上的,C++选手只能干着急.难道C++真的低Pascal一等吗?答案是不言而喻的.一个进阶的方法是把数据一下子读进来,然后再转化字符串,这种方法传说中很不错,但具体如何从没试过,因此今天就索性把能想到的所有的读数据的方式都测试了一边,结果是惊人的. 竞赛中读

对于scanf和cin的输入输出速度的验证

本文为https://www.byvoid.com/zhs/blog/fast-readfile的验证性文章 --------------------------------------------------------------------------- 首先生成一千万个随机数 1 #include<cstdio> 2 #include<ctime> 3 #include<cstdlib> 4 int main () 5 { 6 freopen("dat

Error:全局变量不明确(using namespace std 与全局变量的冲突)

在用递归写八皇后时,定义了一个全局变量count,结果出现问题如下:提示全局变量不明确. 最后发现在实现文件.cpp中,我使用了using  namespace std; 解决方法: 1.使用count 的地方改成 ::count 替代(因为std命名空间下也有 std::count ,编译器不确定此处是::count 还是std::count,所以会不明确) 2.注释掉这个命名空间 3.或者改用 : using std::cout; using std::endl; using std::ci