C++Primer 习题3.6-3.20
string、vector的非迭代器用法
#include<iostream>
#include<string>
using namespace std
;
int main()
{
for (char c
: str1
)
{
str2
+= 'X';
}
str1
= str2
;
cout
<< str1
<< endl
;
return 0;
}
#include<iostream>
#include<string>
using namespace std
;
int main()
{
string str1
{ "woshinibaba!" };
for (int i
= 0; i
< str1
.size(); i
++)
str1
[i
] = 'X';
cout
<< str1
<< endl
;
string str2
{ "woyeshinibaba!" };
int i
= 0;
while (i
< str2
.size())
{
str2
[i
] = 'X';
i
++;
}
cout
<< str2
<< endl
;
i
= 0;
return 0;
}
#include<iostream>
#include<string>
using namespace std
;
int main()
{
string str1
= { "A11!,)+_ b b" };
for (auto c
: str1
)
if (!ispunct(c
))
cout
<< c
;
return 0;
}
#include<iostream>
#include<string>
using namespace std
;
int main()
{
const string str
= "keep out";
for (auto a
: str
)
{
cout
<< a
;
}
return 0;
}
#include<iostream>
#include<vector>
using namespace std
;
int main()
{
vector
<int> vint
;
for (int a
; cin
>> a
;)
{
char f
;
vint
.push_back(a
);
cout
<< "继续吗?F退出" << endl
;
cin
>> f
;
if (f
== 'F')
break;
}
for (auto a
: vint
)
cout
<< a
<< " ";
return 0;
}
#include<iostream>
#include<vector>
#include<string>
using namespace std
;
int main()
{
vector
<string
> vint
;
for (string a
; cin
>> a
;)
{
int f
;
vint
.push_back(a
);
cout
<< "继续吗?1退出" << endl
;
cin
>> f
;
if (f
== 1)
break;
}
for (auto a
: vint
)
cout
<< a
<< " ";
return 0;
}
#include<iostream>
#include<vector>
using namespace std
;
int main()
{
vector
<int> vint
{ 1,2,33,444 };
cout
<< "容器大小为:" << vint
.size() << endl
;
cout
<< "容器内元素为:";
for (auto a
: vint
)
cout
<< a
<< " ";
return 0;
}
#include<iostream>
#include<vector>
using namespace std
;
int main()
{
vector
<int> vint1
{ 42,42,42,42,42,42,42,42,42,42 };
vector
<int> vint2(10, 42);
vector
<int> vint3
;
for (int i
= 0; i
< 10; i
++)
{
vint3
.push_back(42);
}
for (auto a
: vint1
)
cout
<< a
<< " ";
cout
<< endl
;
for (auto a
: vint2
)
cout
<< a
<< " ";
cout
<< endl
;
for (auto a
: vint3
)
cout
<< a
<< " ";
cout
<< endl
;
return 0;
}
转载请注明原文地址:https://tech.qufami.com/read-23465.html