简体   繁体   English

如何从向量C ++中的字符串指向第n个字符

[英]How to point to n'th char from string in vector C++

When I iterate over a vector using an int (say i), I can easily say: 当我使用int遍历向量时(例如i),我可以轻松地说:

vector<string> V;
V[i][g]

in which (int) g is the g'th character of the string in V[i] 其中(int)g是V [i]中字符串的第g个字符

When I am in a loop and want to keep iterating although I will delete items (from V) on the run, I want to make use of: 当我处于循环中并希望继续进行迭代时,尽管我会删除运行中的项目(从V中删除),但我想使用:

vector<string>::iterator it;

and then, I thought, the g'th character of V[i] would - in the loop - be: 然后,我认为V [i]的第g个字符在循环中应该是:

for (it = V.begin() ; it != V.end() ; it++)
    *it[g]

or, more logically in my eyes: 或者,更合乎逻辑的是:

    it[g]

bit neither work... Can anyone tell me how to get the g'th char of V[i] in this iterator-using variant? 一点都不起作用...谁能告诉我如何在使用迭代器的变量中获得V [i]的第g个字符?

What you want to do is 你想做的是

for (std::vector<std::string>::iterator it = V.begin(); it!=V.end(); it++) {
        std::cout << *it << std::endl;       // whole string
        std::cout << (*it)[2] << std::endl;  // third letter only
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM