Skip to content

Instantly share code, notes, and snippets.

@farma11
Created January 14, 2018 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farma11/d9e48d0c64d39fada9e1b7a8c4bbabd2 to your computer and use it in GitHub Desktop.
Save farma11/d9e48d0c64d39fada9e1b7a8c4bbabd2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <sstream>
using namespace std;
int main(){
string str = "acm-icpc", sub;
//文字列の反転 ->イテレータを用いて範囲指定
reverse(str.begin(), str.end());
cout << "反転: " << str << " (" << str.size() << ")" << endl;
//文字列の末尾に挿入
str.append(3, '!');
cout << "末尾に!を3つ挿入: " << str << endl;
//文字列の最後の削除 ->イテレータで指定
str.erase(str.end()-1); //end()は最後の次を指す
cout << "末尾削除: " << str << " (" << str.size() << ")" << endl;
//途中の文字削除 ->イテレータで指定
str.erase(str.begin()+3); //begin()は先頭を指す
cout << "4文字目削除: " << str << " (" << str.size() << ")" << endl;
//アスキーコードの操作
char plus = 5; //'A'はC言語はint型 C++はchar型
str[1] += plus;
cout << "ascii: " << str << endl;
//文字(列)の検索 ->添え字を返す(文字列は最初の添え字)
cout << "最初のuc: " << str.find("uc")+1 << "文字目から" << endl;
cout << "最後のc: " << str.find_last_of('c')+1 << "文字目" << endl;
cout << "存在しない: " << str.find("e")+1 << endl; //string::npos
//文字のカウント
int count = 0;
for(unsigned int i = 0; i < str.size(); i++){
if(str[i] == 'c') count++;
}
cout << "カウント: c " << count << "個" << endl;
//文字列のカウント
count = 0;
string s = "mc";
for(unsigned int i = 0; i < str.size() - s.size() + 1; i++){
if(str.substr(i, s.size()) == s) count++; //添え字iからs.size()分
}
cout << "カウント: " << s << " " << count << "個" << endl;
//部分的にコピー ->開始文字の添え字と文字数指定
sub = str.substr(2, 3);
cout << "3文字目から3文字コピー: "<< sub << endl;
//部分的に削除->開始文字の添え字を文字数の指定
str.erase(2, 3);
cout << "3文字目から3文字削除: "<< str << endl;
//文字列を挿入->開始場所の添え字を文字列指定
str.insert(2, sub);
cout << "3文字目から3文字挿入: "<< str << endl;
//文字を交換
swap(str, sub);
cout << "交換: str = " << str << " sub = " << sub << endl;
//文字列内部の文字同士の交換
swap(sub[0], sub[sub.size()-1]); //size()は文字数
cout << "先頭と末尾の交換: " << sub << endl;
//内部的に辞書順にソート
sort(sub.begin(), sub.end());
cout << "昇順: " << sub << endl;
sort(sub.begin(), sub.end(), greater<char>());
cout << "降順: " << sub << endl;
//文字列内の順列を表示
vector<int> p; count = 0;
for(unsigned int i = 0; i < str.size(); i++) p.push_back(i);
do {
cout << ++count << ": ";
for(unsigned int i = 0; i < str.size(); i++){
cout << str[p[i]];
if(i == str.size() - 1){ //出力の体裁
if( count % 3 != 0 ) cout << " ";
else cout << endl;
}
}
} while (next_permutation(p.begin(), p.end()));
//文字列から整数へ変換
string num = "12345";
int n = atoi((const char*)num.c_str());
num += "-5"; n += -5; //共に-5を足す
cout << "整数へ変換: " << num << " = " << n << endl;
//文字列から1桁ずつ整数へ変換
num = "12345";
char number[10];
int sum = 0;
strcpy(number, num.c_str());
for(unsigned int i = 0; i < num.size(); i++){
sum += number[i] - '0';
}
cout << "1〜5の和: " << sum << endl;
//数字を0埋めで文字列かする場合
ostringstream sout;
sout << setfill('0') << setw(5) << sum;
cout << "5桁にあわせて0埋め; " << sout.str() << endl;
//文字列内の空白削除
size_t pos;
string blank = "w i n";
while((pos = blank.find(" ")) != string::npos){
blank.erase(pos, 1);
}
cout << "空白削除: " << blank << endl;
}
/* 出力結果
反転: cpci-mca (8)
末尾に!を3つ挿入: cpci-mca!!!
末尾削除: cpci-mca!! (10)
4文字目削除: cpc-mca!! (9)
ascii: cuc-mca!!
最初のuc: 2文字目から
最後のc: 6文字目
存在しない: 0
カウント: c 3個
カウント: mc 1個
3文字目から3文字コピー: c-m
3文字目から3文字削除: cuca!!
3文字目から3文字挿入: cuc-mca!!
交換: str = c-m sub = cuc-mca!!
先頭と末尾の交換: !uc-mca!c
昇順: !!-acccmu
降順: umccca-!!
1: c-m 2: cm- 3: -cm
4: -mc 5: mc- 6: m-c
整数へ変換: 12345-5 = 12340
1〜5の和: 15
5桁にあわせて0埋め; 00015
空白削除: win
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment