2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

istringstream stringを空白ごとに区切れる!?

Posted at

istringstreamとは?

istringstreamは,文字列を入力ストリームとして扱い,その中から特定のデータを効率的に抽出するために使用します.つまり,空白で分けられるだけでなく,入力時のようにデータごとに型を割り振ることができる!!
実際の具体例を以下に書きます.

.cpp
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
    string input = "123 Hello World";
    istringstream iss(input);

    int num;
    string word1;
    string word2;
    
    iss >> num >> word1 >> word2;

    cout << "Number: " << num << endl;
    cout << "Word: " << word1 << " " << word2 << endl;

    return 0;
}

出力

Number: 123
Word: Hello World

istringstreamを使うには

#include <iostream>
#include <sstream>

が必ず入ります!!

それでは,みなさんいいエンジニアライフを〜👋

2
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?