へんてこのブログ

日々気づいたことや、最近やっていることを書いています

SRMまとめ

はてなダイアリーの方にも書いていますが、よく見るのでBlogの方にも書いておきます。

stringからintへ変換

int num = (int) string_hoge - '0';

上記じゃ出来ない場合あり

#include<sstream>
#include<string>
int main() {
  string str = "666";
  stringstream ss;
  int n;
  ss << str;
  ss >> n;
}

intからstringへ変換

int sum = 5;
stringstream ss;
ss << sum;
string s = ss.str();

stringからconst *charへ変換

string s = "hoge";
const char* p_str;
p_str = s.c_str();
char* c = *p_str;  

charからint

int n = c - 48;

vectorのsort

//charもsortできるよ!!!!!!!!!(SRM536Div2 500問題より)
//stringもね☆
sort(data.begin(),data.end());