1. istream
- setw(): 입력받는 글자 수 제한,
- 아래에 있는 코드를 Run 해보면 setw를 거친 buf의 문자들이 저장되는지 확인해 볼 수 있다.
int main(){
char buf[5];
cin >> setw(5) >> buf;
cout << buf << endl;
cin >> setw(5) >> buf;
cout << buf << endl;
cin >> setw(5) >> buf;
cout << buf << endl;
}
- cin은 빈칸을 무시한다.
- 만약 빈칸을 받고 싶다면 cin.get()을 사용
- cin.getline(): 줄바꿈 캐릭터까지 받아옴
- cin.gcount(): 입력값의 개수
- cin.ingnore: 입력 값을 무시
- cin.peek(): 입력 값을 peek한만큼만 보여줌
- cin.unget(): 읽어오고 남은 값을 기억함
- cin.putback(): 입력한 갑 뒤에 추가로 입력해줌.
2. ostream
- cout.setf(std::ios::showpos): + 기호 출력
- cout.unsetf(std::ios::showpos): setf로 출력된 값 지움
- 이런식으로 출력값에 10진수 16진수로 변환할 수 도 있음
#include <iostrea>
#include <iomanip>
int main(){
cout << std::hex;
cout << 108 << endl;
cout << 109 << endl;
cout << std::dec;
cout << 108 << endl;
}
- 이 이외에도 많은 플래그들이 존재 래퍼런스를 찾아보길(반올림, 불리언 등등)
- 어떻게 출력해야하는지에 대한 규칙을 찾고 필요한 걸 찾자
3. 문자열 스트림
- 문자열에 대해 스트림의 연산을 수행할 수 있게 된다.
#include <iostream>
#include <sstream>
int main(){
stringstream os;
os << "Hello, World!";
os.str("Hello, World!");
}
4. 흐름상태와 입력 유효성 검증
- 사용자 입력이 예상하는 형식과 일치하는지 확인하는 방법
void printStatus(const std::ios& stream){
cout << boolalpha;
cout << "good()=" << stream.good() << endl;
cout << "eof()=" << stream.fail() << endl;
cout << "bad()=" << stream.bad() << endl;
}
- isdigit(), isblack(), isalpah()도 있다.
- 최적화를 위해 비트마스크를 사용할 때 도 있다.
5. 정규 표현식 소개
int main(){
return 0;
while(true){
getline(cin, str);
if(std::regex_match(str,re))
cout << "Match" << endl;
else
cout << "No Match << endl;
{
auto begin = std::sregex_iterator(str.begin(), str.end(), re);
audo end = std::sregex_iterator();
for (auto itr = begin; itr != end; ++itr){
std::smatch match = *itr;
cout << match.str() << " ";
}
cout << endl;
}
cout << endl;
}
}
6. 기본적인 파일 입출력
std::ofstream outfile("example.txt");
outfile << "파일에 쓰기 예제";
outfile.close();
std::ifstream infile("example.txt");
std::string content;
infile >> content;
std::cout << content << std::endl;
infile.close();
- 바이너리 파일 입출력에선 바이너리라는 것을 신경 써야한다.
7. 파일의 임의 위치 접근하기
- 접근할 파일을 지정하거나 파일내에서 접근할 위치를 지정한다.
std::fstream file("example.txt");
file.seekg(10, std::ios::beg);
std::string data;
file >> data;
std::cout << data << std::endl;
file.close();