Skip to content

Instantly share code, notes, and snippets.

@MasazI
Created May 30, 2015 09:57
Show Gist options
  • Save MasazI/c1329f6b480598c74d60 to your computer and use it in GitHub Desktop.
Save MasazI/c1329f6b480598c74d60 to your computer and use it in GitHub Desktop.
stringstream.cpp
//
// 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