字符串迭代器 [英] String Iterators
问题描述
大家好,
我想从std :: string中获取char *(不是const)。
我看到字符串。 begin()返回一个迭代器,它是实现
定义。
和* iterator返回对元素的引用
那么& * iterator返回一个指向元素的指针?
现在这个编译但是合法吗?
Adrian
#include< iostream>
#include< string>
#include< locale>
int main(int argc,char * argv [])
{
std :: string mixed(" We Are a test String");
const std :: ctype< char& ctype = std :: use_facet< std :: ctype< char>
>(std :: locale :: classic());
std :: cout<<混合<< std :: endl;
//从迭代器获取指针的好方法
//你被允许这样做吗? />
ctype.tolower(& * mixed.begin(),& * mixed.end());
std :: cout<<混合<< std :: endl;
//而不是这个
ctype.toupper(& mixed [0],& mixed [mixed.length() ]);
std :: cout<<混合<< std :: endl;
返回0;
}
Adrian写道:
我想要一个来自std :: string的char *(不是const)。
我看到string.begin()返回一个迭代器,它是实现
定义。
和* iterator返回对元素的引用
所以和& *迭代器返回一个指向元素的指针?
是的。
现在这个编译但是合法吗?
>
Adrian
#include< iostream>
#include< string>
#include< locale>
int main(int argc,char * argv [])
{
std :: string mixed(" ;我们是一个测试字符串");
const std :: ctype< char& ctype = std :: use_facet< std :: ctype< char>
>(std :: locale :: classic());
std :: cout<<混合<< std :: endl;
//从迭代器获取指针的好方法
//你被允许这样做吗? />
ctype.tolower(& * mixed.begin(),& * mixed.end());
这假定字符串将其字符保存在数组中。我不认为这是标准中任何地方的保证。为什么你不能只用''变换'来赚钱?
std :: transform(mixed.begin(),mixed.end() ,mixed.begin(),tolower);
?只是好奇。你当然可以写自己的''tolower''
会使用你的特定方面,不是吗?
std :: cout<<混合<< std :: endl;
//而不是这个
ctype.toupper(& mixed [0],& mixed [mixed.length() ]);
std :: cout<<混合<< std :: endl;
返回0;
}
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要'不要问
4月18日下午2:00,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
Adrian写道:
这假设字符串将其字符保存在阵列。我不认为这是标准中任何地方的保证。
它没有,但21.3.2确实表示它们符合序列容器
,其中不幸的是所有阵列都不存在。
那么这也适用于使用& mixed [0]和
& mixed [mixed.length()]
为什么你不能简单地使用''转换''?
因为我试图找出有用的方面。会很好
for stl中的一个简单的tolower / toupper函数正好用于
字符串:-)
Point现在虽然没什么 - 这是一个旧的编译器并且不支持
locale'无论如何
std :: transform(mixed.begin( ),mixed.end(),mixed.begin(),tolower);
?只是好奇。你当然可以写自己的''tolower''
会使用你的特定方面,不是吗?
我正在寻找已经在STL中的东西,这样它比我能写的任何东西都更有效率。
Adrian
Adrian写道:
4月18日,2:00下午,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
> Adrian写道:
这假设字符串将其字符保存在数组中。我不认为标准中的任何地方都能保证这一点。
它没有,但21.3.2确实表示它们符合序列容器
,其中不幸的是所有阵列都不存在。
那么这也适用于使用& mixed [0]和
& mixed [mixed.length()]
什么都不适用?重载的operator []返回一个引用。
你可以获取它的地址来获取引用的
对象的地址。
>
>为什么你不能简单地使用''transform''?
因为我试图找出有用的方面。会很好
for stl中的一个简单的tolower / toupper函数正好用于
字符串:-)
Point现在是没有意义 - 它是一个旧的编译器,并且不支持
locale''s
> std :: transform(mixed.begin(),mixed.end(),mixed.begin(),
tolower);
?只是好奇。你当然可以写自己的''tolower'',
会使用你的特定方面,不是吗?
我正在寻找已经在STL中的东西,这样它比我能写的任何东西都更有效率。
你基本上花了你宝贵的时间试图找到一些难以理解的解决方案(可能存在,也可能不存在)您可能会或可能不会遇到一些
性能问题。那个漂亮的
总结了你想要做的事情吗?
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
Hi all,
I want a char * (not const) from a std::string.
I see string.begin() return an iterator which is implementation
defined.
And *iterator returns a reference to the element
So does &*iterator return a pointer to the element?
Now this below compiles but is it legal?
Adrian
#include <iostream>
#include <string>
#include <locale>
int main(int argc, char *argv[])
{
std::string mixed("We ARE a test String");
const std::ctype<char &ctype=std::use_facet<std::ctype<char >
>(std::locale::classic());
std::cout << mixed << std::endl;
// What is a good way to get the pointer from an iterator
// Are you allowed to do this?
ctype.tolower(&*mixed.begin(), &*mixed.end());
std::cout << mixed << std::endl;
// instead of this
ctype.toupper(&mixed[0], &mixed[mixed.length()]);
std::cout << mixed << std::endl;
return 0;
}
Adrian wrote:I want a char * (not const) from a std::string.
I see string.begin() return an iterator which is implementation
defined.
And *iterator returns a reference to the element
So does &*iterator return a pointer to the element?Yes.
Now this below compiles but is it legal?
Adrian
#include <iostream>
#include <string>
#include <locale>
int main(int argc, char *argv[])
{
std::string mixed("We ARE a test String");
const std::ctype<char &ctype=std::use_facet<std::ctype<char >>(std::locale::classic());
std::cout << mixed << std::endl;
// What is a good way to get the pointer from an iterator
// Are you allowed to do this?
ctype.tolower(&*mixed.begin(), &*mixed.end());This assumes that the string keeps its characters in an array. I
don''t think this is guaranteed anywhere in the Standard. Why can''t
you simply use ''transform''?
std::transform(mixed.begin(), mixed.end(), mixed.begin(), tolower);
? Just curious. You can of course write your own ''tolower'' that
would use your specific facet, can''t you?
std::cout << mixed << std::endl;
// instead of this
ctype.toupper(&mixed[0], &mixed[mixed.length()]);
std::cout << mixed << std::endl;
return 0;
}V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
On Apr 18, 2:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:Adrian wrote:
This assumes that the string keeps its characters in an array. I
don''t think this is guaranteed anywhere in the Standard.It doesnt but 21.3.2 does say they conform to sequence containers
which unfortunantley arent all arrays either.
Then also doesnt this apply to using &mixed[0] and
&mixed[mixed.length()]
Why can''t you simply use ''transform''?Because I was trying to find out how useful facets are. Would be nice
for a simple tolower/toupper functions in the stl that just worked on
strings :-)
Point is moot now though - its an old compiler and doesn support
locale''s anyway
std::transform(mixed.begin(), mixed.end(), mixed.begin(), tolower);
? Just curious. You can of course write your own ''tolower'' that
would use your specific facet, can''t you?I was looking for something already in the STL so that it would be
more efficent than anything I could write.
Adrian
Adrian wrote:On Apr 18, 2:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:>Adrian wrote:
This assumes that the string keeps its characters in an array. I
don''t think this is guaranteed anywhere in the Standard.It doesnt but 21.3.2 does say they conform to sequence containers
which unfortunantley arent all arrays either.
Then also doesnt this apply to using &mixed[0] and
&mixed[mixed.length()]Doesn''t WHAT apply? The overloaded operator[] returns a reference.
You can take the address of it to get the address of the referred
object.
>>Why can''t you simply use ''transform''?Because I was trying to find out how useful facets are. Would be nice
for a simple tolower/toupper functions in the stl that just worked on
strings :-)
Point is moot now though - its an old compiler and doesn support
locale''s anyway
> std::transform(mixed.begin(), mixed.end(), mixed.begin(),
tolower);
? Just curious. You can of course write your own ''tolower'' that
would use your specific facet, can''t you?I was looking for something already in the STL so that it would be
more efficent than anything I could write.You''re essentially spending your precious time trying to find some
elusive solution (which may or may not exist) for the sake of some
performance problem you may or may not even have. Does that pretty
much sum up what you''re trying to do here?
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于字符串迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!