Is pointer arithmetic possible with C++ string class?

可紊 提交于 2019-12-04 05:10:57

问题


After programming a little in C I decided to jump right into C++. At first I was pleased with the presence of the string class and being able to treat strings as whole units instead of arrays of characters. But I soon found that the C-style strings had the advantage of letting the program move through it character by character, using pointer arithmetic, and carry out a desired logical operation.

I have now found myself in a situation that requires this but the compiler tells me it is unable to convert from type string to the C-style strings. So I was wondering, is there a way to use pointer arithmetic to reference single characters or to pass arguments to a function as the address of the first character while still using the string class without having to create arrays of characters or do I just want to have my cake and eat it too?


回答1:


string characters can be accessed by index, pointers, and through the use of iterators.

if you wanted to use iterators, you could make a function that checks whether a string has a space in it or not:

bool spacecheck(const string& s)
{
    string::const_iterator iter = s.begin();
    while(iter != s.end()){
        if (isspace(*iter))
            return true;
        else
            ++iter;
    }
}

At the beginning of the function, I initialized an iterator to the beginning of the string s by using the .begin() function, which in this case returns an iterator to the first character in a string. In the while function, the condition is that iter != s.end(). In this case end() returns in iterator referring to the element after the last character of the string. In the body, (*iter), which is the value pointed to by iter, is sent to the function isspace(), which checks if a character is a space. If it fails, iter is incremented, which makes iter point to the next element of the string.

I am learning c++ myself and by writing all of this stuff out it has helped my own understanding some. I hope I did not offend you if this all seemed very simple to you, I was just trying to be concise.

I am currently learning from Accelerated c++ and I could not recommend it highly enough!




回答2:


You can use &your_string[0] to get a pointer to the initial character in the string. You can also use your_string.begin() to get an iterator into the string that you can treat almost like a pointer (dereference it, do arithmetic on it, etc.)

You might be better off telling us more about what you're trying to accomplish though. Chances are pretty good that there's a better way to do it than with a pointer.

Edit: For something like counting the number of vowels in a string, you almost certainly want to use an algorithm -- in this case, std::count_if is probably the most suitable:

struct is_vowel {
    bool operator()(char ch) { 
        static const char vowels[] = "aeiouAEIOU";
        return strchr(vowels, ch) != NULL;
    }
};

int vowels = std::count_if(my_string.begin(), my_string.end(), is_vowel());

We're still using begin(), but not doing any pointer(-like) arithmetic on it.



来源:https://stackoverflow.com/questions/5130020/is-pointer-arithmetic-possible-with-c-string-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!