1. getline()
#include<iostream>
getline(cin,temp,delim);
- cin is the standard input stream function
- temp is the variable name used to store characters
- delim is the end sign
example
Input: 10.70.44.68, split the number
string ip;
cin>>ip;
istringstream iss(ip);
string seg;
vector<int> v;
while(getline(iss,seg,'.')) v.push_back(stoi(seg));
string seg;
vector<int> v;
while(getline(cin,seg,'.')) v.push_back(stoi(seg));
二、cin.getline()
#include<iostream>
getline(char * s,n);
getline(char * s,n,delim);
- s is a character array, such as char name[100]
- n is the number of characters to be read
- delim is the end mark, default is newline character
example
#include <iostream>
using namespace std;
int main () {
char name[256]
cout << "Please, enter your name: ";
cin.getline (name,256);
cout << name << endl;
return 0;
}
Three, the difference
- getline() is a function of string stream and can only be used for string type input operations. When you define a string type variable, you can only use getline() to input it.
- cin.getline is a function of std stream, used for char type input operations. When you define a char type variable, it can only be input using cin/cin.getline().