先ほどジャンル違いで質問してしまったので、再度C/C++の方へ質問いたします。
string str = "AABBCCDD";
これを以下のように分割する方法が知りたいです。
result[0] = "AA";
result[1] = "BB";
result[2] = "CC";
result[3] = "DD";
分割する固定長2と、結果を保存するstring型のresultを与えれば
以下のようになる関数を作りたいのですが、
C++の標準機能でスマートにできる方法がベストです。
Perlでいうunpack見たいなものです。
use strict;
my $str = "AABBCCDD";
my @result = unpack '(a2)*', $str; // こんなことをC++でしたい
foreach my $val (@result) {
print $val, "\n";
}
よろしくお願いします。
No.1ベストアンサー
- 回答日時:
たとえばこんなの。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string cut(string& str, int n) {
string result = str.substr(0,n);
str.erase(0,n);
return result;
}
int main() {
vector<string> output;
string input = "AABBCCDD";
while ( !input.empty() ) {
output.push_back(cut(input,2));
}
for ( int i = 0; i < output.size(); ++i ) {
cout << output[i] << endl;
}
}
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
このQ&Aを見た人はこんなQ&Aも見ています
-
許せない心理テスト
私は「あなたの目の前にケーキがあります。ろうそくは何本刺さっていますか」と言われ「12本」と答えたら…
-
フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
あなたが普段思っている「これまだ誰も言ってなかったけど共感されるだろうな」というあるあるを教えてください
-
映画のエンドロール観る派?観ない派?
映画が終わった後、すぐに席を立って帰る方もちらほら見かけます。皆さんはエンドロールの最後まで観ていきますか?
-
海外旅行から帰ってきたら、まず何を食べる?
帰国して1番食べたくなるもの、食べたくなるだろうなと思うもの、皆さんはありますか?
-
天使と悪魔選手権
悪魔がこんなささやきをしていたら、天使のあなたはなんと言って止めますか?
-
TextFieldParserの固定長桁数をバイト単位で指定したい
Visual Basic(VBA)
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
- ・ゆるやかでぃべーと タイムマシンを破壊すべきか。
- ・歩いた自慢大会
- ・許せない心理テスト
- ・字面がカッコいい英単語
- ・これ何て呼びますか Part2
- ・人生で一番思い出に残ってる靴
- ・ゆるやかでぃべーと すべての高校生はアルバイトをするべきだ。
- ・初めて自分の家と他人の家が違う、と意識した時
- ・単二電池
- ・チョコミントアイス
このQ&Aを見た人がよく見るQ&A
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
UNICODE環境でCStringをstd::st...
-
VS2019でofstreamが未定義になる
-
VC++で文字列から任意の文字を...
-
_tcscat がうまくいきません(V...
-
なぜ、C++の標準ヘッダをインク...
-
途中からMFCを使用したい
-
STLでポインタのリストをsort()...
-
#include "fstream.h"
-
#defineの使い方について
-
JPEGやPNGが読めるLoadImage関数
-
includeファイルが開けない
-
構造体配列のvectorへの変換と...
-
string型からint型への型変換
-
C++型変換について
-
C言語 MACアドレス
-
VHDLのsignedとunsignedの違いは?
-
STLで、vectorのファイルへの書...
-
std::vectorのメモリ管理を自分...
-
2÷3などの余りについて
-
C言語 エラーの原因がわからな...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
switch文のエラーについて
-
構文エラーが出ているのですが...
-
なぜ、C++の標準ヘッダをインク...
-
gccでコンパイル時のエラー
-
空ENTERの判別
-
【C++】ヘッダ内でstringを格納...
-
snprintfが使用できない。
-
enumの値から定義名を文字列化...
-
#include "fstream.h"
-
C++で、テキストファイルを一行...
-
std::wstringのメモリリークに...
-
C++での <iostream.h>と<iostre...
-
VC++で文字列から任意の文字を...
-
#defineの使い方について
-
wstringの主力
-
vc++の使い方について
-
std::map の const 修飾について
-
string型のフォーマット書式指...
-
構造体配列のvectorへの変換と...
-
指定した文字を削除したい
おすすめ情報