#include <iostream>
#include <string>
using namespace;
int main(void)
{
string s = "assd";
cout << s.size() << '\n';
return 0;
}
stoi : string to int
stol : string to long
stoll : string to long long
stof : string to float
stod : string to double
stold : string to long double
stoul : string to unsigned long
stoull : string to unsigned long long
to_string
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
char ch[10] = "hello";
string s1(ch);
string s2 = ch;
cout << s1 << " " << s2 << '\n';
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s = "hello";
char ch[10];
strcpy(ch, s.c_str());
cout << ch <<'\n';
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string a = "0123456789abcdefghij";
// count 가 npos 이므로 pos 부터 문자열 끝까지 리턴한다.
std::string sub1 = a.substr(10);
std::cout << sub1 << '\n';
// pos 와 pos + count 모두 문자열 범위 안이므로, 해당하는 부분 문자열을
// 리턴한다.
std::string sub2 = a.substr(5, 3);
std::cout << sub2 << '\n';
// pos 는 문자열 범위 안이지만, pos+count 는 밖이므로, pos 부터 문자열 끝까지
// 리턴한다.
std::string sub4 = a.substr(a.size() - 3, 50);
std::cout << sub4 << '\n';
try {
// pos 가 문자열 범위 밖이므로 예외를 발생시킴.
std::string sub5 = a.substr(a.size() + 3, 50);
std::cout << sub5 << '\n';
} catch (const std::out_of_range& e) {
std::cout << "pos exceeds string size\n";
}
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "hello world";
if(str.find("world") == string::npos){
cout << "true" << '\n';
}
return 0;
}
#include <iostream>
#include <string>
int main(void)
{
int arr[5];
memset(arr, 1, sizeof(arr));
for (int i = 0; i < 5; ++i)
{
cout << arr[i] << '\n';
}
return 0;
}