두번째 스터디 : STL - gomamon/twitch_algorithm GitHub Wiki

#include <stdio.h>

int main(){
	
}

namespace

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it.

이름충돌을 방지하기위해 사용됨!

Identifiers outside the namespace can access the members by using the fully qualified name for each identifier, for example

#include <iostream>

namespace SPACE_A
{
	int num = 5
}
  
namespace SPACE_B
{
	int num = 3;
}
  
int main()
{
	int num = 1;

	cout << "main score : " << std::num << endl;
	cout << "A_RANGE score : " << SPACE_A::num << endl;
	cout << "B_RANGE score : " << SPACE_B::num << endl;
	return 0;
}

//Header.h
#include <string>

namespace Test1
{
	std::string Func() { return std::string("Hello world"); }
}

namespace Test2
{
	std::string Func() { return std::string("Bye world") }
}

#include "header.h"
#include <string>
#include <iostream>

int main()
{
    using namespace Test1;

    string s = Func();
    std::cout << s << std::endl; // "Hello world"
    return 0;
}

using namespace std;

std⇒ standard 표준 네임스페이스 사용

#include <iostream>
#include <string>
using namespace std;

int main(){

	string s = "hey";
}

STL(Standard Template Library)

The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc.

http://www.cplusplus.com/

stl components

  • Algorithms
    • sorting
    • searching
  • Containers
    • vector
    • map
    • stack
    • queue
  • Functions
    • function
  • Iterators
    • iterators
  • Utility Library
    • pair

Container

Vector

#include

메모리 블록에 연속하게 저장

앞쪽에는 원소추가제거 x but 뒤에 추가 제거 가능

  • push_back(element)

  • pop_back()

  • resize()

  • begin()

  • end()

  • size()

    #include #include using namespace std;

    int main(){ vector v1; vector v2(7); //7개의 원소를 갖는 벡터 vector v3(7,-1); //-1로 초기화된 7개의 원소를 갖는 벡터

    
    v1.push_back(0); // 0
    v1.push_back(1); // 0 1
    v1.pop_back();   // 0
    
    v1.resize(5);  // 0 1 0 0 0 
    v1.begin();
    v1.end();
    

    vector < int > vv1[100];
    
    vector <vector < int > > vv2;
    vv2.resize(100)
    

    }

String

#include

#include <iostream>
#include <string>
using namespace std;

int main(){

	string s1;
	string s2 = "hello";

	cin >> s1; //"hello"

	cout << "1 :" << s1 << endl ; //1: hello

	printf("2 :%c\n", s1[2]); //2: l

	cout << "3 :" << s1.empty() << endl; //3: 0

	/* == != + += 가능*/
	if(s1 == s2){
			s1.push_back('!');// hello! 
	}

	cout << "4 :" << s1 << endl; //4: hello!
	
	printf("5: %s\n", s1.c_str());   //5: hello!


​ /* substr(pos, len)*/ ​ cout << "6 :" << s1.substr(2,2) << endl;
​ // 6: ll ​

}

Map

#include

std::map<key, value>

트리구조로 되어있음

자료를 저장하고 찾아야할때 사용

삽입과 동시에 정렬! insert, delete, search O(log n)

  • void clear()

  • size_t size()

  • bool empty()

    #include #include #include using namespace std; int main(){

    map < string , int> pokemon;

    pokemon["pikachu"] = 1; pokemon["bulbasaur"] = 2; pokemon["squirtle"] = 3; pokemon["charmander"] = 4; pokemon["Butterfily"] = 5;

    cout << pokemon["squirtle"] << endl; //3

    cout << pokemon["pidgeotto"] << endl; //0

    /* Butterfily:5 bulbasaur:2 charmander:4 pikachu:1 squirtle:3 */

    }

Stack

#include

last in first out

  • push()

  • pop()

  • size()

  • top()

  • empty()

    // CPP program to demonstrate working of STL stack #include #include using namespace std;

    void showstack(stack s) { while (!s.empty()) { cout << '\t' << s.top(); s.pop(); } cout << '\n'; }

    int main () { stack s; s.push(10); //10 s.push(30); //30 10 s.push(20); //20 30 10 s.push(5); // 5 20 30 10 s.push(1); // 1 5 20 30 10

    cout << "The stack is : "; 
    showstack(s); 
      
    cout << "\ns.size() : " << s.size(); 
    cout << "\ns.top() : " << s.top(); 
    
    
    
    
    
    cout << "\ns.pop() : "; 
    a = s.top()
    	s.pop(); 
    showstack(s); 
      
    return 0; 
    

    }

    /* The stack is : 1 5 20 30 10

    s.size() : 5 s.top() : 1 s.pop() : 5 20 30 10 */

Queue

#include

first in first out

  • push()

  • pop()

  • front()

  • back()

  • empty()

  • size()

    #include #include

    using namespace std;

    void showq(queue gq) { queue g = gq; while (!g.empty()) { cout << '\t' << g.front(); g.pop(); } cout << '\n'; }

    int main() { queue gquiz; gquiz.push(10); //10 gquiz.push(20); // 10 20 gquiz.push(30); //10 20 30

    cout << "The queue gquiz is : "; 
    showq(gquiz); 
      
    cout << "\ngquiz.size() : " << gquiz.size(); 
    cout << "\ngquiz.front() : " << gquiz.front(); 
    cout << "\ngquiz.back() : " << gquiz.back(); 
      
    cout << "\ngquiz.pop() : "; 
    gquiz.pop(); 
    showq(gquiz); 
      
    return 0; 
    

    }

    /* The queue gquiz is : 10 20 30

    gquiz.size() : 3 gquiz.front() : 10 gquiz.back() : 30 gquiz.pop() : 20 30 */

Iterator

container의 원소를 순회힐 수 있는 객체

begin() : beginning position of the container

end() : after end position of container

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){

	map <string, int> pokemon;

  pokemon["pikachu"] = 0;
	pokemon["bulbasaur"] = 1;
	pokemon["squirtle"] = 2;
	pokemon["charmander"] = 3;
	pokemon["Butterfily"] = 4;

	//map <string, int>::iterator
	for( auto iter = pokemon.begin()
			;iter != pokemon.end()
			; iter++ ) {
        cout << iter->first << ':' << iter->second << endl;
    }

	/*
	Butterfily:4
	bulbasaur:1
	charmander:3
	pikachu:0
	squirtle:2
	*/
}

Algorithm

#include

  • swap

  • min

  • max

  • sort

    #include // std::cout #include // std::swap #include

    	using namespace std;
    	
    	int main () {
    	
    	  int x=10, y=20, z=30;         // x:10 y:20
    	  swap(x,y);        // x:20 y:10    
    	
    	printf("%d\n",max(x,y)) //20
    	printf("%d\n", min(x, min(y, z))) //10
    	
    	vector <int> v;
    	
    	for(int i=9; i>=0; i--){
    		v.push_back(i);
    	}
    	// 9 8 7 6 5 4 3 2 1 0
    	
    	sort(v.begin(), v.end());//0 1 2 3 4 5 6 7 8 9 
    	sort(v.begin(), v.end(), gerater<int>())
    	
    	int M = max(v[0], v[8]);
    	M = max(v[0], max(v[1], v[2]));
    

    }

pair

The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second).

Pair is used to combine together two values which may be different in type.

비교연산 가능

#include <iostream> 
#include <utility>  // #include <algorithm>
using namespace std; 
  
int main() 
{ 
    pair <int, string> P ; 
 
    P.make_pair(100, "pikachu");

    cout << P.first << " " ; //100
    cout << P.second << endl ; //pikachu
  
    return 0; 
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector < pair<int, int > > II

int main(){
	for(int i=0; i<10; i++){
		II.push_back(make_pair(i, (-1)*i ))
	}
	for (int i=0; i<10; i++){
		printf("%d %d", II[i].first, II[i].second);
	}
	// 0 0 1 -1 2 -2
}
⚠️ **GitHub.com Fallback** ⚠️