다익스트라 알고리즘

  • 한정점에서 모든정점까지의 거리를 알고 싶을때 사용

 

플로이드와샬 알고리즘

  • 모든정점에서 모든정점까지의 거리를 알고 싶을때 사용

 

'Algorithm' 카테고리의 다른 글

완전탐색  (0) 2020.01.27
지역변수로 크기가 큰 배열 선언 시 문제점  (0) 2019.09.27
Prim vs Dijkstra (프림 다익스트라 비교)  (4) 2019.09.23

string은 문자열을 메모리에 저장할 때 실제론 '\0' 을 포함하지만, 개념상 \0 에 대한 접근을 허용하지 않는다.

 

string s = "LOVE YOU", 문자열을 단어 단위로 저장하려고 할때 이를 사용할 수 있다.

 

string s = "I LOVE YOU BABY";
string temp;
vector<string> v;
 
for(int i = 0; s[i] != '\0'; i++)
{
    if(s[i] == ' ')
    {
        v.push_back(temp);
        temp.clear();
    }
 
    temp += s[i];
}
v.push_back(temp);
cs

 

출처 :
https://hashcode.co.kr/questions/5777/c-string-클래스에-문자열을-저장할-때는-널문자가-없나요

 

 

 

#include <string>

1. compare : 문자열 비교

#include <iostream>
#include <string>
using namespace std;
 
string s1 = "LOVE";
string s2 = "ABC";
 
int result = s1.compare(s2);   // 양수 반환
cs
  • s1 = s2 : 0 반환
  • s1 > s2 : 양수 반환(1)
  • s1 < s2 : 음수 반환(-1)

 

2. find : 문자열 검색 -> STL의 find함수랑 다름

#include <iostream>
#include <string>
using namespace std;
 
string s = "LOVE";
 
int idx1 = s.find("LO");      // 0 반환 
int idx2 = s.find("VE"1);   // 2 반환
 
if(s.find("AC"== string::npos)
{
   "AC" 문자열을 못찾으면 TRUE
}
 
if(s.find("LO") != string::npos)
{
   "LO" 문자열을 찾으면 TRUE
}
cs
  • s.find("문자열") : 찾은 문자열의 인덱스 반환
  • s.find("문자열", 시작 인덱스) : 시작 인덱스부터 찾은 문자열의 인덱스 반환

 

3. erase : 문자열 삭제

#include <iostream>
#include <string>
using namespace std;
 
string s = "LOVE";
 
s.erase(12);                           // "OV" 삭제 -> s = "LE"
s.erase(find(s.begin(), s.end(), "L"));   // "L" 삭제 -> s = "E"
cs
  • s.erase(시작 인덱스) : 시작 인덱스부터 끝까지 삭제
  • s.erase(시작 인덱스, 개수) : 시작 인덱스부터 개수만큼 삭제
  • s.erase(반복자) : 반복자가 가르키는 인덱스부터 1만큼 삭제

 

4. substr : 문자열 나누기

#include <iostream>
#include <string>
using namespace std;
 
string s = "LOVE";
 
string s2 = s.substr(2);      // s = "VE"
string s3 = s.substr(13);   // s = "OVE"
cs
  • s.substr(시작 인덱스) : 시작 인덱스부터 끝까지 나눈 문자열 반환
  • s.substr(시작 인덱스, 개수) : 시작 인덱스부터 개수만큼 나눈 문자열 반환

 

5. replace : 문자열 변경

#include <iostream>
#include <string>
using namespace std;
 
string s = "LOVE";
 
s.replace(12"AA");   // s = "LAAE"
cs
  • s.replace(시작 인덱스, 개수, "문자열") : 시작 인덱스부터 개수만큼 문자열 변경

 

6. insert : 문자열 삽입

#include <iostream>
#include <string>
using namespace std;
 
string s = "LOVE";
 
s.insert(2"aa"); // s = "LOaaVE"
cs
  • s.insert(시작 인덱스, "문자열") : 시작 인덱스에 문자열 삽입

 

7. push_back / pop_back : 문자 삽입 / 삭제

#include <iostream>
#include <string>
using namespace std;
 
string s = "LOVE";
 
s.pop_back();        // s = "LOV";
s.push_back('E');    // s = "LOVE";
cs
  • s.push_back('문자') : 문자열 맨 끝에 문자 1개 삽입
  • s.pop_back() : 문자열 맨 끝에 문자 1개 삭제

 

8. to_string : int, float, long 등 -> string 변환

#include <iostream>
#include <string>
using namespace std;
 
int a = 524;
string s = to_string(a);   // s = "524"
cs

 

9. stoi : string -> int 변환 (C++11 이상)

10. atoi(s.c_str()) : string -> int 변환 (C++11 이하)

#include <iostream>
#include <string>
using namespace std;
 
string s = "524";
int a = stoi(s);   // a = 524
 
 
string s = "524";
int a = atoi(s.c_str());   // a = 524
cs

 

 

 

#include <algorithm>

1. find : 원소 검색 in <vector>

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
 
1) array and pointer
int a[5= {12345};
int *ap = find(a, a+54);      // 찾으면 a+3 반환, 못찾으면 a+5 반환
 
char b[5= "LOVE";
char *bp = find(b, b+5'O');   // 찾으면 b+1 반환, 못찾으면 b+5 반환
 
2) vector and iterator
vector<string> v;
v.push_back("LOVE");
v.push_back("YOU"); 
v.push_back("ME"); 
v.push_back("COOL");
 
vector<string>:: iterator it;
it = find(v.begin(), v.end(), "YOU");   // 찾으면 "YOU" 가르키는 반복자 반환, 못찾으면 v.end() 반환
cs
  • find(시작, 종료, 원소) : 찾으면 원소를 가르키는 반복자 반환, 못찾으면 종료 반환

 

2. reverse : 역순

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
 
int a[5= {12345};
char a[5= "LOVE";
string s = "LOVE";
 
reverse(a+1, a+5);             // (a+1)~(a+5)까지 역순 a[5] = {1, 5, 4, 3, 2}
reverse(a, a+4);               // 널문자 제외
reverse(s.begin(), s.end());   // s = "EVOL"
cs
 

 

방법 1.

#include <vector>

using namespace std;

 

vector<int> arr;                       // arr 1차원벡터 선언

arr.assign(m, 0);                       // arr[m] 할당, 0으로 초기화

 

vector<vector<int>> arr;           // arr 2차원벡터 선언

arr.assign(m, vector<int>(n, 0));   // arr[m][n] 할당, 0으로 초기화 

 

방법 2.

#include <vector>

using namepspace std;

 

vector<vector<int>> arr(m, vector<int>(n, 0));   // arr 2차원벡터 선언 및 arr[m][n] 할당, 0으로 초기화 

 

+ Recent posts