cin.get()的作用问题?
请问下,下面的c++ plus plus的代码中,cin.get()是什么作用?注释掉了,在for循环处就只能输入一次循环。为什么会这样。
// vect1.cpp -- introducing the vector template
#include <iostream>
#include <string>
#include <vector>
const int NUM = 5;
int main()
{
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
vector<int> ratings(NUM);
vector<string> titles(NUM);
cout << "You will do exactly as told. You will enter\n"
<< NUM << " book titles and your ratings (0-10).\n";
int i;
for (i = 0; i < NUM; i++)
{
cout << "Enter title #" << i + 1 << ": ";
getline(cin,titles[i]);
cout << "Enter your rating (0-10): ";
cin >> ratings[i];
cin.get();}
cout << "Thank you. You entered the following:\n"
<< "Rating\tBook\n";
for (i = 0; i < NUM; i++)
{
cout << ratings[i] << "\t" << titles[i] << endl;
}
return 0;
}
-----------------------
std::get(1) template <size_t I, class... Types>
typename tuple_element< I, tuple<Types...> >::type& get(tuple<Types...>& tpl) noexcept;
(2) template <size_t I, class... Types>
typename tuple_element< I, tuple<Types...> >::type&& get(tuple<Types...>&& tpl) noexcept;
(3) template <size_t I, class... Types>
typename tuple_element< I, tuple<Types...> >::type const& get(const tuple<Types...>& tpl) noexcept;
Get element
Returns a reference to the Ith element of tuple tpl.
Version (2), which takes an rvalue reference of a tuple as argument, applies forward to the returned element.
Version (3), which takes a const tuple as argument, returns a const reference to the element.
get can also be used on tuple-like objects like pair and array (see pair's and array's specializations).
Notice that get uses a template parameter, I, to determine which element to be gotten. Template parameters must be constexpr (compile-time const values).
Template parameters
I
Position of an element in the tuple, with 0 as the position of the first element.
size_t is an unsigned integral type.
Types
Types of the elements in the tuple (generally obtained implicitly from tpl).
Function parameters
tpl
A tuple object with more than I elements.
Return value
A reference to the element at the specified position in the tuple.
tuple_element< I, tuple<Types...> >::type is the type of the Ith element in the tuple.
-----------------------
用法1: cin.get(字符变量名)可以用来接收字符
#include <iostream>
using namespace std;
main ()
{
char ch;
ch=cin.get(); //或者cin.get(ch);
cout<<ch<<endl;
}
输入:jljkljkl
输出:j
用法2:cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格
#include <iostream>
using namespace std;
main ()
{
char a[20];
cin.get(a,20);
cout<<a<<endl;
}
输入:jkl jkl jkl
输出:jkl jkl jkl
输入:abcdeabcdeabcdeabcdeabcde (输入25个字符)
输出:abcdeabcdeabcdeabcd (接收19个字符+1个'\0')
[解决办法]
#include <iostream>
#include <string>
#include <vector>
const int NUM = 3;
int main()
{
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
vector<int> ratings(NUM);
vector<string> titles(NUM);
cout << "You will do exactly as told. You will enter\n"
<< NUM << " book titles and your ratings (0-10).\n";
int i;
for (i = 0; i < NUM; i++)
{
cout << "Enter title #" << i + 1 << ": ";
getline(cin,titles[i]);
cout << "Enter your rating (0-10): ";
cin >> ratings[i];
char ch = cin.get();//这个为了读掉行末的回车。。你改成这样就明白了。。
cout<<int(ch)<<endl;
}
cout << "Thank you. You entered the following:\n"
<< "Rating\tBook\n";
for (i = 0; i < NUM; i++)
{
cout << ratings[i] << "\t" << titles[i] << endl;
}
return 0;
}