auto 修飾子を使用した文字列操作
#include<bits/stdc++.h>
using namespace std;
string s1;
string s2(s1);
string s3("hello");
string s4="hello";
void cp5_1(){
string s1="helloworld";
string s2("helloworld");
string s3(10,'c');
}
void cp5_2(){
string s1;
cin >> s1;
}
void cp5_3(){
string s1;
while(getline(cin,s1)){
cout << s1 << endl;
}
if(s1.empty()) cout << "null";
if(s1.size()!=0) cout << s1;
size_t st = s1.size();
}
void cp5_4(){
string s1="hello";
string s2="helloworld";
string s3="hey";
if(s2>s1) cout << "yes!" << endl;
if(s3>s2) cout << "not" << endl;
}
void cp5_5(){
string s1="hey",s2="tom";
string s3=s1+"john";
string s4=(s1+"not")+"think";
}
void cp5_6(){
cout << isalnum('a') << endl;
cout << isalpha('1') << endl;
cout << isdigit('3') << endl;
cout << isspace(' ') << endl;
cout << islower('a') << isupper('B') << endl;
cout << ispunct('.') << endl;
}
void cp5_7(){
string str("helloworld");
for(auto c:str){
cout << c << "\t";
}
cout << endl;
for(auto &c:str){
c=toupper(c);
cout << c << "\t";
}
cout << endl;
}
ベクター
#include<bits/stdc++.h>
#include<vector>
using namespace std;
vector<double> cp1;
vector<vector<int> > cp2;
vector<int> cp3_1;
vector<int> cp3_2 = cp3_1;
vector<string> s1{
"abc","hello","boy"};
vector<string> s2(10,"hey");
vector<double> d1(10);
vector<double> d2{
10};
void cp6_1(){
string word;
vector<string> str;
while(cin >> word){
str.push_back(word);
}
}
void cp6_2(){
vector<unsigned> num(11,0);
cout << num[1] << endl;
}
イテレータ
#include<bits/stdc++.h>
using namespace std;
void cp7_1(){
string s("helloworl");
auto it = s.begin();
cout << *it <<endl;
}
void cp7_2(){
string s1("some body");
if(s1.begin()!=s1.end()){
auto it = s1.begin();
*it = toupper(*it);
}
cout << *(s1.end()-1) <<endl;
}
void cp7_3(){
string s1("hello");
string::iterator si1 = s1.begin();
vector<string> s2(10,"str");
vector<string>::const_iterator sci1 = s2.begin();
}
void cp7_4(){
vector<int> v1{
1,39,221};
auto v2 = v1.cbegin();
vector<int>::const_iterator v3 = v1.cend();
}
void cp7_5(){
vector<string> s1{
"hey"};
vector<string>::iterator s2 = s1.begin();
if(!(*s2).empty() && !(s2+1)->empty()){
cout << "got it!" <<endl;
}
}
void cp7_6(){
vector<string> s1{
"helloworld","hey","thanks","tombs"};
auto s2 = s1.begin();
cout << *s2 << *(s2+1) <<endl;
auto s3 = (s2+3)-s2;
}
void important(){
vector<int> text(10,0);
auto beg = text.begin(), end = text.end();
auto mid = text.begin() + (end - beg)/2;
}
配列反復子と参照
#include<iostream>
#include<cstddef>
using namespace std;
void cp8_1(){
int c0[10];
int *c1[10];
int (*c2)[10]=&c0;
int (&c3)[10]=c0;
}
void cp8_2(){
int a[3][2]={
{
3,2},{
0,9},{
34,213}};
for(auto i:a){
cout<<a<<" ";
}
cout << endl;
}
void cp8_3(){
int c1[4]={
1,2,3,4};
int *c2 = &c1[4];
for(int *b=c1;b!=c2;++b){
cout << *b << endl;
}
}
void cp8_4(){
int c1[4]={
1,2,3,4};
int *c2 = begin(c1);
int *c3 = end(c1);
}
void cp8_5(){
constexpr size_t sz = 5;
constexpr ptrdiff_t pt = 4;
}
void cp8_6(){
double a[3]={
3.44,2.239,9.541};
double *b=a,*c=a+3;
while(b<c){
cout << *b++ << " " ;
}
cout<<endl;
}
void cp8_7(){
int a[5]={
1,2,3,4,5};
int *p = &a[2];
int k = p[-2];
}
残りの文字列
void cp9_1(){
string s1="helloworld";
string s2="hey";
if(s1>s2) cout<<"yes"<<endl;
char c[]={
'a','b','\0'};
}
void cp9_2(){
string s1="nothing";
const char *str=s1.c_str();
int num[]={
1,2,3,4,5,6};
vector<int> num2(begin(num),end(num));
}