C++的 Vector 容器中 string 类型元素可否用一种指针替代它?

2019-09-20 00:20:02 +08:00
 kirara

代码目的是把迭代器第一个 string 类型元素的第一段不包括空格的字符全改为大写。
期望输出:SOME string oh
实际输出:SOME string oh
代码输出确实是正确的,但是总觉得代码里太多解引用(*it)写起来很繁琐,不知道大佬们怎么解决。如题。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	vector<string> s{ "some string"," oh" };
	for (auto it = s.begin(); it != s.end() && !it->empty(); ++it) {
		if (it == s.begin()) { //想把(*it)改成字符串直接使用;
			for (decltype((*it).size()) index = 0; index != (*it).size() && !isspace((*it)[index]); index++) {
				(*it)[index] = toupper((*it)[index]);
			}
		}
		cout << (*it) << " ";
	}
	return 0;
}
2255 次点击
所在节点    程序员
10 条回复
littlespider89
2019-09-20 00:23:30 +08:00
为啥不用指针写法 it->size()
mind3x
2019-09-20 00:27:13 +08:00
既然是只处理第一个 string,没看懂外面一圈 for 是拿来干嘛的
kirara
2019-09-20 00:44:35 +08:00
@mind3x 写着玩的,别在意
inhzus
2019-09-20 00:51:58 +08:00
这个代码的可读性真的震惊到我了…果然对 c++来说 一千个人心中有一千个哈姆雷特。
1. 既然只处理第一个,判断不为空,直接取 s[0] 不久好了吗。
2. 第二个循环既然和 index 的位置无关,用 ranged-based for 不也会让代码简洁很多吗。
3. 大家写 c++ 也会像楼主这样把 size_type 用 decltype 进行 type deduction 吗?这个写法虽然没问题,不过感觉不符合我的习惯…
aijam
2019-09-20 00:52:45 +08:00
for (auto &str: s) {
for (auto it = str.begin(); it != str.end() && !isspace(*it); it++) {
*it = toupper(*it);
}
cout << str << " ";
}
wbing
2019-09-20 01:00:33 +08:00
```
if(s.size() == 0) return -1;
std::stringstream ss(s[0]);
std::string str;
if(ss >> str)
{
auto index = s[0].find_first_of(str);
std::transform(str.begin(),str.end(),str.begin(),::toupper);
s[0].replace(index,str.size(),str);
}
```
lishunan246
2019-09-20 01:14:03 +08:00
这里就两个 for 循环,一个用 iterator,一个用 index ;一个用++i,一个用 i++。
size_t 写成 decltype((*it).size())。
《如何把你写 c++的朋友气到脑中风》
tianshilei1992
2019-09-20 01:47:53 +08:00
这样如何?
```C++
#include <iostream>
#include <string>
#include <vector>

using namespace std;

inline void func(vector<string> &vec) {
if (vec.empty()) {
return;
}

auto& str = vec.front();

for (auto itr = str.begin(); *itr != ' ' && itr != str.end(); ++itr) {
*itr = toupper(*itr);
}
}

int main(int argc, char *argv[]) {
vector<string> vec = {"some string", " oh"};
func(vec);

for (const auto& str : vec) {
cout << str << ' ';
}

cout << endl;

return 0;
}
```
tianshilei1992
2019-09-20 01:48:50 +08:00
@tianshilei1992 Hot fix:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

inline void func(vector<string> &vec) {
if (vec.empty()) {
return;
}

auto& str = vec.front();

for (auto itr = str.begin(); itr != str.end() && *itr != ' '; ++itr) {
*itr = toupper(*itr);
}
}

int main(int argc, char *argv[]) {
vector<string> vec = {"some string", " oh"};
func(vec);

for (const auto& str : vec) {
cout << str << ' ';
}

cout << endl;

return 0;
}
jmc891205
2019-09-20 08:51:14 +08:00
你可能想要 range-based for loop
for (auto str: s) {cout << str << endl;}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/602340

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX