C++ 可以进行指针算术吗?字符串类?
在使用 CI 编程了一些之后,决定直接转向 C++。起初,我对字符串类的存在以及能够将字符串视为整个单元而不是字符数组感到满意。但我很快发现 C 风格字符串的优点是可以让程序使用指针算术逐个字符地遍历它,并执行所需的逻辑操作。
我现在发现自己处于需要此操作的情况,但编译器告诉我它无法从字符串类型转换为 C 样式字符串。所以我想知道,有没有一种方法可以使用指针算术来引用单个字符或将参数作为第一个字符的地址传递给函数,同时仍然使用字符串类而不必创建字符数组,或者我只是想有我的蛋糕也吃吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
字符串字符可以通过索引、指针和使用迭代器来访问。
如果你想使用迭代器,你可以创建一个函数来检查字符串中是否有空格:
在函数的开头,我将迭代器初始化为字符串的开头 s通过使用 .begin() 函数,在本例中返回一个指向字符串中第一个字符的迭代器。在 while 函数中,条件是 iter != s.end()。在这种情况下,end() 在迭代器中返回,引用字符串最后一个字符之后的元素。在正文中,iter 指向的值 (*iter) 被发送到函数 isspace(),该函数检查字符是否为空格。如果失败,iter 就会递增,这使得 iter 指向字符串的下一个元素。
我自己正在学习 C++,通过写出所有这些东西,它帮助我自己理解了一些。如果这一切对你来说似乎很简单,我希望我没有冒犯你,我只是想简洁一点。
我目前正在学习 Accelerated c++,我极力推荐它!
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:
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!
您可以使用
&your_string[0]
获取指向字符串中初始字符的指针。您还可以使用your_string.begin()
获取字符串中的迭代器,您可以将其视为指针(取消引用它,对其进行算术运算等)。您最好告诉我们更多关于你想要实现的目标。很可能有比指针更好的方法来做到这一点。
编辑:对于像计算字符串中元音数量这样的事情,您几乎肯定想要使用一种算法 - 在这种情况下,
std::count_if
可能是最合适的:我们仍在使用
begin()
,但不对它执行任何指针(类似)算术。You can use
&your_string[0]
to get a pointer to the initial character in the string. You can also useyour_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:We're still using
begin()
, but not doing any pointer(-like) arithmetic on it.