c++のメモ

概要

c++についての自分用のメモ書き

g++

g++ がサポートしている規格

$ g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 $  g++ -v --help 2>/dev/null | grep -E "^\s+\-std=.*$"
  -std=<standard>          Assume that the input sources are for <standard>
  -std=f2003                  Conform to the ISO Fortran 2003 standard
  -std=f2008                  Conform to the ISO Fortran 2008 standard
  -std=f2008ts                Conform to the ISO Fortran 2008 standard
  -std=f95                    Conform to the ISO Fortran 95 standard
  -std=gnu                    Conform to nothing in particular
  -std=legacy                 Accept extensions to support legacy code
  -std=c++03                  Conform to the ISO 1998 C++ standard revised by
  -std=c++0x                  Deprecated in favor of -std=c++11
  -std=c++11                  Conform to the ISO 2011 C++ standard
  -std=c++1y                  Conform to the ISO 201y(7?) C++ draft standard
  -std=c++98                  Conform to the ISO 1998 C++ standard revised by
  -std=c11                    Conform to the ISO 2011 C standard (experimental
  -std=c1x                    Deprecated in favor of -std=c11
  -std=c89                    Conform to the ISO 1990 C standard
  -std=c90                    Conform to the ISO 1990 C standard
  -std=c99                    Conform to the ISO 1999 C standard
  -std=c9x                    Deprecated in favor of -std=c99
  -std=gnu++03                Conform to the ISO 1998 C++ standard revised by
  -std=gnu++0x                Deprecated in favor of -std=gnu++11
  -std=gnu++11                Conform to the ISO 2011 C++ standard with GNU
  -std=gnu++1y                Conform to the ISO 201y(7?) C++ draft standard
  -std=gnu++98                Conform to the ISO 1998 C++ standard revised by
  -std=gnu11                  Conform to the ISO 2011 C standard with GNU
  -std=gnu1x                  Deprecated in favor of -std=gnu11
  -std=gnu89                  Conform to the ISO 1990 C standard with GNU
  -std=gnu90                  Conform to the ISO 1990 C standard with GNU
  -std=gnu99                  Conform to the ISO 1999 C standard with GNU
  -std=gnu9x                  Deprecated in favor of -std=gnu99
  -std=iso9899:1990           Conform to the ISO 1990 C standard
  -std=iso9899:199409         Conform to the ISO 1990 C standard as amended in
  -std=iso9899:1999           Conform to the ISO 1999 C standard
  -std=iso9899:199x           Deprecated in favor of -std=iso9899:1999
  -std=iso9899:2011           Conform to the ISO 2011 C standard (experimental

引数

引数を2倍して出力する

参考 - C++で文字列を数値に変換する方法 - C++と色々 - http://www7b.biglobe.ne.jp/~robe/cpphtml/html03/cpp03042.html

https://github.com/ludwig125/work/blob/master/src/cpp/test/argv.cpp

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char** argv)
{
    for(int i = 1; i < argc; i++){
        cout << "argv "<< i << " : " << argv[i] << endl;
        cout << "double argv: " << atoi(argv[i])*2 << endl;
    }
    return 0;
}

実行結果

$ g++ argv.cpp 
[~/git/work/src/cpp/test ] $ ./a.out 13 83
argv 1 : 13
double argv: 26
argv 2 : 83
double argv: 166

型変換

型を確認

#include <iostream>                                                     
#include <stdlib.h>
#include <typeinfo>
using namespace std;

int main(int argc, char** argv)
{
    int a,b;
    for(int i = 1; i < argc; i++){
        cout << "argv "<< i << " : " << argv[i] << endl;
    }

  // 引数の型
    cout << typeid(argv[1]).name() << " : " << argv[1] << endl;

    a = atoi(argv[1]);
    b = atoi(argv[2]);

    // 変換後の型
    cout << typeid(a).name() << " : " << a << endl;

    return 0;
}

実行結果

$ ./a.out 10 30
argv 1 : 10
argv 2 : 30
Pc : 10
i : 10

typeidの出力

Uses of typeid in C++ - Codeforces

bool = b
char = c
unsigned char = h
short = s
unsigned short = t
int = i
unsigned int = j
long = l
unsigned long = m
long long = x
unsigned long long = y
float = f
double = d
long double = e
string = Ss
int[] = A_i
double[] = A_d
vector<int> = St6vectorIiSaIiE

c++のstringへの変換について

to_stringが使えない

あまり考えずにto_stringを使おうとしたら、以下のコンパイルエラーが出た

error: ‘to_string’ is not a member of ‘std’

c++11にするとか、コンパイル時の指定が必要とか書いてある c++ - "to_string" isn't a member of "std"? - Stack Overflow

面倒なので他の方法を探してみる

stringstreamを使う

stringstreamを使った方法が見つかった

参考 - [O] C++ で int 型の値を string 型にするときは stringstream - 文字列⇔数値 - code snippets - C++11で数字→文字列はstd::to_string()、文字列→数字はstd::stoi()とかstd::stod()とか - minus9d's diary

やってみる

超適当なコード

$ cat stringToint.cpp 
#include <iostream>
#include <string>

#include <sstream>

int main(void) {
    int   a = 3;
    float b = 3.14;
    double c = 2.99792458;
    std::string d= "this is test";

    std::string str;
    std::stringstream ss;    

    ss << a;
    str = ss.str();
    std::cout << str << std::endl;

    ss.str("");
    ss << b;
    str = ss.str();
    std::cout << str << std::endl;

    ss.str("");
    ss << c;
    str = ss.str();
    std::cout << str << std::endl;

    ss.str("");
    ss << d;
    str = ss.str();
    std::cout << str << std::endl;
}

実行結果

$ g++ stringToint.cpp
$ ./a.out 
3
3.14
2.99792
this is test

vector

vectorのソート

参考: - http://blog.sarabande.jp/post/62062199465 - http://kaworu.jpn.org/cpp/std::sort

https://github.com/ludwig125/work/blob/master/src/cpp/test/sort_vector.cpp

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
                                                                               
    vector<int> v;
    v.push_back ( 3 );
    v.push_back ( 4 );
    v.push_back ( 1 );
    v.push_back ( 2 );

    std::sort(v.begin(), v.end(), std::greater<int>() );
    for(vector<int>::iterator it = v.begin(); it != v.end(); it++){
        cout << *it << endl;
    }

    return 0;
}

実行結果

[~/git/work/src/cpp/test ] $ ./a.out 
4
3
2
1
[~/git/work/src/cpp/test ] $

文字列を受け取って1文字ずつvectorに格納

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string str;
    getline(cin,str);

    string tmp;
    // 'abcde'のような文字列を一文字ずつvectorに格納
    vector<string> v;
    for(int i = 0; i < (int)str.size(); i++){
        char ch = str[i];
        cout << ch << " " << endl;

//        v.push_back(string(ch));
        // 直接charをvectorに格納しようとしたらコンパイルエラーになったのでstringの変数に入れた
        tmp = ch;
        v.push_back(tmp);
    }

    cout << "output vector" << endl;
    for(vector<string>::iterator it = v.begin(); it != v.end(); it++){
        cout << *it << endl;
    }

    return 0;
}