1112실습 - kyagrd/cprog2018Fall GitHub Wiki

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

using namespace std;

/*
참조형, bool 타입 기본적으로 지원, 객체지향 프로그래밍, 제너릭 프로그래밍,
표준라이브러리:
    가변 길이 문자열 타입 (string)
    일반적인 가변 길이 배열 (vector) 모든 타입에 다 적용할 수 있는 제너릭한 구조
*/

int main777(void)
{
    vector<string> svec;

    int n = 20;

    cout << svec.size() <<endl;

    char c = 'a';
    for (int i = 0; i <n; ++i) {
        string s = "hello ";
        svec.push_back(s + c++);
    }

    cout << svec.size() <<endl;

    for (int i = 0; i<n; ++i) {
        cout <<svec[i] <<endl;
    }
    cout <<endl;

    return 0;

}


int main3333(void) {
    // int a[10] = { 1, 2, 3 };
    int n = 100;
    vector<float> v;

    cout << v.size() <<endl;

    for (int i = 0; i <n; ++i) {
        v.push_back(1+i*0.1);
    }

    cout << v.size() <<endl;

    for (int i = 0; i<n; ++i) {
        cout <<v[i] <<' ';
    }
    cout <<endl;

    return 0;
}

// 참조형 reference


template <typename T>
void Swap(T &x, T &y)
{
    T tmp = x;
    x = y;
    y = tmp;
}


int main(void)
{
    int n = 333; // "aaa";
    int m = 444; // "bbb";
    Swap(n, m);

    cout <<n <<' ' <<m <<endl;
/*
//    int *p = &n;
//    int &x = n;

    // n = 6;
    // *p = 7;
//    x = 8;

    cout << n <<endl;
    cout << *p <<endl;
//    cout << x <<endl;
*/
    return 0;
}

int main3(void) {
    // 길이 제한이 없는 추상적인 문자열 타입
/*
    // C 스타일 문자열 - 고정된 메모리 공간
    char cstr[10]; // 9 글짜까지만 정상처리
    cin >> cstr;
*/
    // C++에서는 bool이 기본 타입 C에서는 <stdbool.h> 인클루드해야
    bool b = true;

    string str;

    cin >> str;

    str += " world";

    cout << str << endl;
    cout << str.length() <<endl;
    cout << str[2] <<endl;

    //     << 123 << endl
   //      << 123.333 << endl;
    return 0;
}

int main4() {
    int n;
    float x;
    cin >> n;
    cin >> x;

    cout <<"n = " <<n <<endl <<"x = " <<x <<endl;



    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️