다익스트라 알고리즘
- 한정점에서 모든정점까지의 거리를 알고 싶을때 사용
플로이드와샬 알고리즘
- 모든정점에서 모든정점까지의 거리를 알고 싶을때 사용
'Algorithm' 카테고리의 다른 글
완전탐색 (0) | 2020.01.27 |
---|---|
지역변수로 크기가 큰 배열 선언 시 문제점 (0) | 2019.09.27 |
Prim vs Dijkstra (프림 다익스트라 비교) (4) | 2019.09.23 |
다익스트라 알고리즘
플로이드와샬 알고리즘
완전탐색 (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-클래스에-문자열을-저장할-때는-널문자가-없나요
cin(문자, 문자열 입력) vs getline(문자열 입력) (0) | 2020.01.13 |
---|---|
string 자주 쓰는 멤버함수 정리 + find, reverse 함수 (0) | 2019.09.21 |
1. compare : 문자열 비교
#include <iostream>
#include <string>
using namespace std;
string s1 = "LOVE";
string s2 = "ABC";
int result = s1.compare(s2); // 양수 반환
|
cs |
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 |
3. erase : 문자열 삭제
#include <iostream>
#include <string>
using namespace std;
string s = "LOVE";
s.erase(1, 2); // "OV" 삭제 -> s = "LE"
s.erase(find(s.begin(), s.end(), "L")); // "L" 삭제 -> s = "E"
|
cs |
4. substr : 문자열 나누기
#include <iostream>
#include <string>
using namespace std;
string s = "LOVE";
string s2 = s.substr(2); // s = "VE"
string s3 = s.substr(1, 3); // s = "OVE"
|
cs |
5. replace : 문자열 변경
#include <iostream>
#include <string>
using namespace std;
string s = "LOVE";
s.replace(1, 2, "AA"); // s = "LAAE"
|
cs |
6. insert : 문자열 삽입
#include <iostream>
#include <string>
using namespace std;
string s = "LOVE";
s.insert(2, "aa"); // s = "LOaaVE"
|
cs |
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 |
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 |
1. find : 원소 검색 in <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
1) array and pointer
int a[5] = {1, 2, 3, 4, 5};
int *ap = find(a, a+5, 4); // 찾으면 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 |
2. reverse : 역순
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int a[5] = {1, 2, 3, 4, 5};
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 |
cin(문자, 문자열 입력) vs getline(문자열 입력) (0) | 2020.01.13 |
---|---|
string 문자열 저장시 널문자 유무 (1) | 2019.09.22 |
방법 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으로 초기화