C++ Builder 中是否有 "for str in StringList do" 的等价物?
Is there an equivalent of "for str in StringList do" in C++ Builder?
我想知道 Delphi
是否有等价物
var
StringList: TStringList;
for str in StringList do
在 C++ 生成器中。
编译器或 IDE 无关紧要。您的 std::string
使用什么容器?
一般来说,比如用std::vector
和std::string
,就可以写
std::vector<std::string> strings;
// fill them
for(auto& string : strings)
这适用于很多其他容器和 类。
编辑:原文没有透露任何关于TStringList
的信息,不过,代码应该很容易移植,if TStringList
提供迭代器。
与其他答案所说的不同,编译器很重要。只有 C++Builder 可以使用 Visual Component Library 中的类型,它是用 Delphi (Object Pascal) 编写的。
TStringList 就是这样一个 class。它是 System::String 实例的容器(有一些额外的)(即不是 std::vector 包含 std ::string 个实例)。
TStringList 不公开 C++ 迭代器,所以 AFAIK,目前,您唯一的选择是索引:
TStringList *list = new TStringList();
// fill list
for (int i = 0; i < list->Count; ++i)
{
// ...
}
delete list;
您当然可以使用 std::vector<System::String>
,但这不会为您提供 TStringList 的附加功能(例如关联对象的存储——例如位图——或 key=value 字符串的特殊处理等)。
更新
我一定是做错了什么。正如 Remy Lebeau 指出的那样,这实际上是可能的:
TStringList *list = new TStringList();
list->Add("Hello");
list->Add("World");
for (System::String str : list)
{
std::wcout << str.c_str() << std::endl;
}
根据 Embarcadero 的 DocWiki:
C++ Iterator Support for Delphi Enumerable Types and Containers
The following are guidelines that can be used as a rule of thumb for which iterators are available for a type.
If the type only has a GetEnumerator()
method, you can use Range-for and a limited set of read-only STL Algorithms.
If the type has a subscript operator[]
, you can use Range-for and all the read-only STL Algorithms.
...
...
TRandomIterator::<T,E>
TRandomIterator::<T,E>
is defined for every RTL container that implements the integer subscript operator E operator[ ](int)
and an integer Count
property.
...
TEnumerableIterator<T>
TEnumerableIterator::<T>
is defined for every Delphi RTL container that implements the GetEnumerator()
method. The returned Enumerator Type must also support the GetCurrent()
and MoveNext()
methods. This pattern is frequently used in RTL code, so it covers a good portion of containers.
...
TStringList
满足 GetEnumerator()
和 operator[]
的要求,因此,至少在 CLang-based compilers 中,您可以这样做:
TStringList *StringList = ...;
for (auto str : StringList)
{
...
}
std::for_each(std::begin(StringList), std::end(StringList), ...);
此功能是在 C++Builder 10.1 Berlin 中添加的。
有关详细信息,请参阅 Using Delphi containers from C++
我想知道 Delphi
是否有等价物var
StringList: TStringList;
for str in StringList do
在 C++ 生成器中。
编译器或 IDE 无关紧要。您的 std::string
使用什么容器?
一般来说,比如用std::vector
和std::string
,就可以写
std::vector<std::string> strings;
// fill them
for(auto& string : strings)
这适用于很多其他容器和 类。
编辑:原文没有透露任何关于TStringList
的信息,不过,代码应该很容易移植,if TStringList
提供迭代器。
与其他答案所说的不同,编译器很重要。只有 C++Builder 可以使用 Visual Component Library 中的类型,它是用 Delphi (Object Pascal) 编写的。
TStringList 就是这样一个 class。它是 System::String 实例的容器(有一些额外的)(即不是 std::vector 包含 std ::string 个实例)。
TStringList 不公开 C++ 迭代器,所以 AFAIK,目前,您唯一的选择是索引:
TStringList *list = new TStringList();
// fill list
for (int i = 0; i < list->Count; ++i)
{
// ...
}
delete list;
您当然可以使用 std::vector<System::String>
,但这不会为您提供 TStringList 的附加功能(例如关联对象的存储——例如位图——或 key=value 字符串的特殊处理等)。
更新
我一定是做错了什么。正如 Remy Lebeau 指出的那样,这实际上是可能的:
TStringList *list = new TStringList();
list->Add("Hello");
list->Add("World");
for (System::String str : list)
{
std::wcout << str.c_str() << std::endl;
}
根据 Embarcadero 的 DocWiki:
C++ Iterator Support for Delphi Enumerable Types and Containers
The following are guidelines that can be used as a rule of thumb for which iterators are available for a type.
If the type only has a
GetEnumerator()
method, you can use Range-for and a limited set of read-only STL Algorithms.If the type has a subscript
operator[]
, you can use Range-for and all the read-only STL Algorithms....
...
TRandomIterator::<T,E>
TRandomIterator::<T,E>
is defined for every RTL container that implements the integer subscript operatorE operator[ ](int)
and an integerCount
property.
...
TEnumerableIterator<T>
TEnumerableIterator::<T>
is defined for every Delphi RTL container that implements theGetEnumerator()
method. The returned Enumerator Type must also support theGetCurrent()
andMoveNext()
methods. This pattern is frequently used in RTL code, so it covers a good portion of containers.
...
TStringList
满足 GetEnumerator()
和 operator[]
的要求,因此,至少在 CLang-based compilers 中,您可以这样做:
TStringList *StringList = ...;
for (auto str : StringList)
{
...
}
std::for_each(std::begin(StringList), std::end(StringList), ...);
此功能是在 C++Builder 10.1 Berlin 中添加的。
有关详细信息,请参阅 Using Delphi containers from C++