반응형

+감사로그에서 초 단위로 발생한 로그 개수 조회.

mysql

LOG_DATE 형식 VARCHAR2로 yyyy-mm-dd hh:mm:ss.sss

LOG_DATE에서 처음부터 19자까지만 얻어와서 그룹별로 카운팅.

카운트로 내림차순으로 정렬.


select * from (

select substr(LOG_DATE, 1, 19) as d, count(*) as cnt from AUDIT_LOG group by substr(LOG_DATE, 1, 19) ) A

order by cnt desc ;



반응형

cpp string

#include <string>


+초기화

std::string s0("initial string") ;

std::string s1 ;

std::string s2(s0) ;

std::string s3(s0, 8, 3) ;    // index 8에서 3글자. => str

std::string s6(10, 'x') ;    // xxxxxxxxxx

std::string s7b(s0.begin(), s0.begin()+7) ;   // initial


+이터레이터 ; begin(), end(), rbegin(), rend(), c*


+용량

size() ; 스트링 길이 (char 수)

length() ; 스트링 길이

reserve(size_type n) ; capacity 변경. 기존 길이보다 작게 설정하면 작동하지 않는다. 기존 스트링의 내용이나 길이를 변경하지는 않는다. 

resize(size_type n) ; 크기 리사이즈. 스트링 길이보다 작으면 해당 길이로 줄어들고, 스트링 길이보다 크면, 나머지 부분에는 디폴트로 null 이 채워지고, 특정 문자로 지정하면 해당 문자들이 채워진다.

capacity() ; 메모리 할당 크기. 스트링 길이 이상이며  스트링 길이와 같지 않을 수 있다. (보통 더 크다.)

clear() ; 내용 모두 삭제. 길이가 0으로 된다. 

empty() ; empty check. 스트링 길이가 0이면 true, 아니면 false.


+c_str()

null terminated 문자열 시작 주소를 리턴한다. 

std::string str("hello world") ;

char *cstr = new char[str.length()+1] ;

std::strcpy(cstr, str.c_str()) ;

delete [] cstr ;


+data()

c_str() 과 동일


+[] operator ; char array index로 접근


+copy()

현재 스트링의 일부를 목적지 주소로 복사한다.  리턴값은 복사된 길이.

copy(charT *s, size_type len, sizt_type pos=0) ;

char buf[20] ;

std::string str("Test string...") ;

std::size_type len = str.copy(buf, 6, 5) ;    // str의 index 5부터 6개를 복사.

buf[len]='\0' ;    // null add.

buf -> "string"


+substr() ; 문자열 일부를 복사하여 얻어온다.

문자열 일부를 새로운 string객체로 만들어 리턴한다.

substr(size_type pos=0, size_type len=npos) ;  // 시작 위치, 길이

길이를 생략하면 끝까지 복사.

std::string str2 = str.substr(12, 12) ;    // index 12에서 12문자 복사.

std::string::size_type pos = str.find("live") ;    // live 문자열 시작 위치 인덱스.

std::string str3 = str.substr(pos) ;    // live부터 끝까지 복사된 객체 생성.


+replace() ; 문자열의 일부를 변경한다.

replace(시작위치, 길이, 대체할 문자열) ; 시작위치부터 해당 길이 부분을 대체한다.

시작위치, 길이를 이터레이터로 대체할 수 있다.

replace(시작위치, 길이, 대체할 문자열, 대체문자열의 시작위치, 대체문자열의 길이)  ; 대체문자열에서 일부를 잘라 그것으로 교체한다.

replace(pos, len, char *)

replace(i1, i2, char*)

replace(pos, len, size_type n , charT c) ;  // 대체할 문자열을 지정한 문자 n개로 한다.

replace(i1, i2, first, last) ;    // 모두 이터레이터로 파라미터를 제공.


+append() ; 문자열 추가

append(string& str) ;

append(string &, subpos, sublen) ; // 추가할 문자열의 일부분 지정 가능 (시작위치, 길이)

append(char* )

append(char *, n) ;

append( n, char) 

append(first, last)   // 이터레이터로 추가할 문자열 범위 지정.

+= operator를 사용하여 추가도 가능하다. 

+= operator는 파라미터로 string, char*, char 가능.

std::string name("John") ;

std::string family("smith") ;

name+=" K. " ;

name+=family ;

name+='\n' ;


+find(needle, start, length)

문자열 검색하여 처음 발견된 위치를 리턴. pos 위치 지정하면 해당 위치부터 검색.

없으면 std::string::npos 리턴

std::string::size_type f = str.find(str2) ;    // str에서 str2가 처음 발견된 index 리턴.

f=str.find(str2, f+1)    // 위에서 발견한 다음 인덱스부터 검색.

f=str.find(str2, f+1, n)    // 위에서 발견한 다음 인덱스부터 검색하는데 str2의 n길이 만큼만 비교.


+rfind()

뒤에서 부터 검색.

뒤에서 부터 검색해서 sixth를 찾아 seventh로 변경

std::string key("sixth") ;

std::string::size_type f = str.rfind(key) ;

if (f!=std::string::npos) 

    str.replace(f, key.length(), "seventh") ;


+find_first_of() 

문자열 중에 아무거나 먼저 발견된 곳을 찾는다.

f=str.find_first_of("aeiou") ;    // 앞에서 부터 먼저 나온 모음 위치 인덱스를 리턴.

f=str.find_first_of("aeiou", f+1) ; // 발견된 다음 위치부터 다시 검색.

find_last_of()

find_first_not_of()

find_last_not_of()












'Develop > C&CPP' 카테고리의 다른 글

numeric: accumulate, adjacent_difference, inner_product, partial_sum  (0) 2018.06.01
string function  (0) 2018.05.31
map  (0) 2018.05.29
List 1  (0) 2018.05.28
Array 1  (1) 2018.05.25
반응형

cpp map

키, 값의 쌍으로 구성된 연관 컨테이너.

#include <map>


+constructor

std::map<char, int> first ;

first['a']=10 ;

first['b']=30 ;

first['c'] = 50 ;

std::map<char,int> second (first.begin(), first.end() ) ;

std::map<char,int> thrid (second) ;


std::map<char, int, classcomp> fourth ;

struct classcomp {

 bool operator() (const char&lhs, const char& rhs) const {  return lhs<rhs ; }

} ;


+일반적인 함수

begin(), end(), cbegin, cend, rbegin, rend, crbegin, crend, size(), clear(), empty() 


+at()

키값으로  요소를 찾아 값을 참조로 반환한다. []와 다르게 키가 없으면 익셉션(out_of_range) 발생.

std::map<std::string, int> mymap = { {"alpha", 0}, {"beta", 0}, {"gamma", 0} } ;

mymap.at("alpha") = 10 ;

mymap.at("beta") = 20 ;

mymap.at("gamma")=30 ;

mymap["alpha"]=40 ;


+count()

키 값이 일치하는 요소의 개수를 리턴한다. 따라서 0 또는 1만 리턴된다. 

mymap.count( "alpha" )    ; 해당 키가 존재하면 1, 없으면 0


+emplace()  (C++11)

새로운 요소를 맵에 추가한다. 중복된 키가 존재하면 추가나 업데이트가 되지 않는다. 

std::map<char,int> map1 ;

map1.emplace('x', 100) ;

map1.emplace('y', 200) ;

for (auto x : map1 ) {

std::cout << x.first << ' ' << x.second << std::endl ;

}

+insert()

map1.insert( std::pair<char,int>('a', 100) ) ;


+find()

키로 이터레이터를 찾는다.

std::map<char,int>::iterator it ;

it = map1.find('b') ;

if ( it!= map1.end() ) map1.erase(it) ;    // 키 'b'를 찾아 요소를 삭제한다.


+lower_bound, upper_bound

키로 범위를 정할 때 사용.

키가 a,b,c,d,e 가 있을 때

b~d까지의 범위

itlow = map1.lower_bound('b') ;    // point to 'b'

itup = map1.upper_bound('d') ;    // point to 'e' 주의!

map1.erase(itlow, itup) ;    // 'b'~'d'까지 삭제함. 

남은 요소는 a, e

+erase() ; 요소 삭제

erase(const_iterator pos) ;

erase(const key_type &k) ;

erase(const_iterator first, last) ;








'Develop > C&CPP' 카테고리의 다른 글

string function  (0) 2018.05.31
string  (0) 2018.05.30
List 1  (0) 2018.05.28
Array 1  (1) 2018.05.25
Vector 3  (0) 2018.05.23

+ Recent posts