stringstreamはstringに対して、<< や >> 演算子を提供する。
ostringstreamは出力のみ提供する。逆にistringstreamは入力のみを提供する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// stringstream.cpp | |
// CplusplusPractice | |
// | |
// Created by masai on 2015/05/30. | |
// Copyright (c) 2015年 masai. All rights reserved. | |
// | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
int main(int argc, char* argv[]){ | |
// stringstream | |
std::stringstream ss; | |
ss << "a"; | |
ss << " "; | |
ss << 'b'; // 1文字であればcharを使うこと。strlenの最適化分のコンパイルが効率化できる。 | |
std::cout << ss.str() << std::endl; | |
// ostringstream 書き出し | |
std::ostringstream oss; | |
// 様々な型をstringに流せる | |
oss << "test" << " " << 100; | |
std::cout << oss.str() << std::endl; | |
// istringstream 読み込み | |
std::istringstream iss(oss.str()); | |
int n; | |
std::string s1; | |
std::string s2; | |
// 入力の順番で取得 | |
iss >> s1 >> s2 >> n; | |
std::cout << s1 << std::endl; | |
std::cout << s2 << std::endl; | |
std::cout << n << std::endl; | |
} |
0 件のコメント:
コメントを投稿