null 문자를 사용하여 종료되는 문자 배열에 대한 포인터를 반환하는 basic_string c_str 함수입니다. null 문자 종료가 있는 문자열 값을 갖는 내장 메소드입니다.
C++에서 c_str 함수를 정의하는 구문 -
const Char ptr* c_str() const
기능 정보
C++ STL 라이브러리에 내장된 메서드입니다. 메소드에 매개변수를 전달할 수 없습니다. char 포인터를 반환합니다. 이 포인터는 NULL로 끝나는 문자 배열을 가리킵니다.
예시
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main() {
string s = "I Love Tutorials Point";
int flag = 0;
cout<<"Checking if the string "<<s<<" contains P "<<endl;
for(int i = 0; i < s.size();i++) {
if(s.c_str()[i] == 'P') {
cout<<"The string contains character";
flag = 1;
}
}
if(flag == 0 ) {
cout<<"The string does not contains character";
}
} 출력
Checking if the string I Love Tutorials Point contains P The string contains character