C++ 转换指针引用的字符串

C++ 转换指针引用的字符串,c++,string,pointers,vector,C++,String,Pointers,Vector,我正在学习迭代器,并决定通过迭代字符串向量并将其转换为小写来使用迭代器。在这样做的过程中,我已经到了一个无法找出问题所在的地步。以下是我一直使用的代码: #include <iostream> #include <vector> #include <string> #include <iterator> #include <algorithm> #include <cctype> int main() { std:

我正在学习迭代器,并决定通过迭代字符串向量并将其转换为小写来使用迭代器。在这样做的过程中,我已经到了一个无法找出问题所在的地步。以下是我一直使用的代码:

#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <cctype>

int main()
{
    std::vector<std::string> vec = {"AbBBBcCc", "AFFDDsDCc"};

    for (std::vector<std::string>::iterator it = vec.begin(); it != vec.end(); ++it)
    {
        std::transform(*it->begin(), *it->end(), *it->begin(),
                       [](char c) { return std::tolower(c); });
        std::cout << *it << std::endl;
    }
    return 0;
}
我想不出怎么修理它。我将指针解引用到一个常规字符串变量,并用变量替换转换函数中的指针调用,从而使它工作。我知道这远不是最好的方法,但我只是想知道为什么这段代码不起作用

多谢各位

std::transform(*it->begin(), *it->end(), *it->begin(),
it
这里迭代向量中的
std::string
s,并引用向量中的某个字符串。。它的
->
重载解析为迭代器当前引用的字符串。因此:

it->begin()
调用
std::string::begin()
,该函数将迭代器返回到字符串开头的第一个字符。所以

*it->begin()
为您提供字符串中的第一个字符,
*it->end()
当然是未定义的行为。解除终止迭代器值的限制始终是未定义的行为,因此表达式“
*it->end()
”应该根据其自身的优点让您暂停并思考一下。你显然想做:

  std::transform(it->begin(), it->end(), it->begin(),
                   [](char c) { return std::tolower(c); });
it
这里迭代向量中的
std::string
s,并引用向量中的某个字符串。。它的
->
重载解析为迭代器当前引用的字符串。因此:

it->begin()
调用
std::string::begin()
,该函数将迭代器返回到字符串开头的第一个字符。所以

*it->begin()
为您提供字符串中的第一个字符,
*it->end()
当然是未定义的行为。解除终止迭代器值的限制始终是未定义的行为,因此表达式“
*it->end()
”应该根据其自身的优点让您暂停并思考一下。你显然想做:

  std::transform(it->begin(), it->end(), it->begin(),
                   [](char c) { return std::tolower(c); });

以下守则适用—

   #include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <cctype>

char* lower(char c){

}
int main()
{
    std::vector<std::string> vec = {"AbBBBcCc", "AFFDDsDCc"};

    for (std::vector<std::string>::iterator it = vec.begin(); it != vec.end(); ++it)
    { std::string data=*it;
        std::transform(data.begin(), data.end(), data.begin(),
    [](unsigned char c){ return std::tolower(c); });
        std::cout << data<< std::endl;
    }
    return 0;
}
如果您希望通过使用
*
阻止指针,然后将其转换为小写,还有另一种方法-

std::transform((*it).begin(), (*it).end(), (*it).begin(),
                       [](char c) { return std::tolower(c); });

我上面的三段代码都完成了相同的任务。上面的不同示例将帮助您理解指针的工作和解引用。希望这有助于以下代码正常工作-

   #include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <cctype>

char* lower(char c){

}
int main()
{
    std::vector<std::string> vec = {"AbBBBcCc", "AFFDDsDCc"};

    for (std::vector<std::string>::iterator it = vec.begin(); it != vec.end(); ++it)
    { std::string data=*it;
        std::transform(data.begin(), data.end(), data.begin(),
    [](unsigned char c){ return std::tolower(c); });
        std::cout << data<< std::endl;
    }
    return 0;
}
如果您希望通过使用
*
阻止指针,然后将其转换为小写,还有另一种方法-

std::transform((*it).begin(), (*it).end(), (*it).begin(),
                       [](char c) { return std::tolower(c); });
我上面的三段代码都完成了相同的任务。上面的不同示例将帮助您理解指针的工作和解引用。希望这有帮助