Created
May 30, 2015 09:57
-
-
Save MasazI/c1329f6b480598c74d60 to your computer and use it in GitHub Desktop.
stringstream.cpp
This file contains hidden or 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment