Is there a way to use a tolower() like function on a vector<string> variable in C++?

huangapple go评论95阅读模式
英文:

Is there a way to use a tolower() like function on a vector<string> variable in C++?

问题

I'm trying to load and sort a file alphabetically. To make this easier, I have to convert the characters to lowercase.

Now, do I have to do this character-by-character as I read it into a string array (because tolower() only accepts characters), or can I somehow use it with a vector<string> container?

Here's the code:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <vector>
  5. using namespace std;
  6. int main() {
  7. ifstream file("phoneNumbers.txt");
  8. vector<string> wordStream;
  9. int i = 0;
  10. if (file.fail()) {
  11. cout << "Couldn't open file!";
  12. return 1;
  13. }
  14. while(file >> wordStream[i]) {
  15. wordStream[i] = tolower(wordStream[i]);
  16. ++i;
  17. }
  18. }

Newbie question, I know.

I already tried the std::transform() function that someone recommended on StackOverflow, but still it didn't work.

英文:

I'm trying to load and sort a file alphabetically. To make this easier, I have to convert the characters to lowercase.

Now, do I have to do this character-by-character as I read it into a string array (because tolower() only accepts characters), or can I somehow use it with a vector&lt;string&gt; container?

Here's the code:

  1. #include &lt;iostream&gt;
  2. #include &lt;fstream&gt;
  3. #include &lt;cstring&gt;
  4. #include &lt;vector&gt;
  5. using namespace std;
  6. int main() {
  7. ifstream file(&quot;phoneNumbers.txt&quot;);
  8. vector&lt;string&gt; wordStream;
  9. int i = 0;
  10. if (file.fail()) {
  11. cout &lt;&lt; &quot;Couldn&#39;t open file!&quot;;
  12. return 1;
  13. }
  14. while(file&gt;&gt;wordStream[i]) {
  15. wordStream[i] = tolower(wordStream[i]);
  16. ++i;
  17. }
  18. }

Newbie question, I know.

I already tried the std::transform() function that someone recommended on StackOverflow, but still it didn't work.

答案1

得分: 3

  1. 你的代码有两个问题,`tolower` 在字符串上不起作用,并且你的向量大小为零,所以对于任何`i`的值,`wordStream[i]`都会报错。以下是修复了这些错误的代码:
  2. string temp;
  3. while (file >> temp) { // 读入一个临时字符串
  4. // 将字符串转换为小写
  5. std::transform(temp.begin(), temp.end(), temp.begin(), tolower);
  6. // 将临时字符串添加到向量中
  7. wordStream.push_back(temp);
  8. }
英文:

Your code has two problems, tolower doesn't work on strings, and your vector has a size of zero so wordStream[i] is an error for any value of i. Here's the code fixed of those errors

  1. string temp;
  2. while (file &gt;&gt; temp) { // read to a temporary string
  3. // convert string to lower case
  4. std::transform(temp.begin(), temp.end(), temp.begin(), tolower);
  5. // add temporary string to vector
  6. wordStream.push_back(temp);
  7. }

huangapple
  • 本文由 发表于 2023年5月18日 04:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定