我想要两个不同类型的循环变量.有没有办法让这项工作?
@Override
public T get(int index) throws IndexOutOfBoundsException {
// syntax error on first 'int'
for (Node<T> current = first, int currentIndex; current != null;
current = current.next, currentIndex++) {
if (currentIndex == index) {
return current.datum;
}
}
throw new IndexOutOfBoundsException();
}
Run Code Online (Sandbox Code Playgroud) 通常在迭代字符串(或任何可枚举对象)时,我们不仅对当前值感兴趣,还对位置(索引)感兴趣.要通过使用string::iterator我们必须维护一个单独的索引来实现这一点:
string str ("Test string");
string::iterator it;
int index = 0;
for ( it = str.begin() ; it < str.end(); it++ ,index++)
{
cout << index << *it;
}
Run Code Online (Sandbox Code Playgroud)
上面显示的样式似乎不比'c-style'优越:
string str ("Test string");
for ( int i = 0 ; i < str.length(); i++)
{
cout << i << str[i] ;
}
Run Code Online (Sandbox Code Playgroud)
在Ruby中,我们可以以优雅的方式获取内容和索引:
"hello".split("").each_with_index {|c, i| puts "#{i} , #{c}" }
Run Code Online (Sandbox Code Playgroud)
那么,C++中迭代可枚举对象并跟踪当前索引的最佳实践是什么?
我以为可以在for循环中初始化几个变量:
for (int i = 0, char* ptr = bam; i < 10; i++) { ... }
Run Code Online (Sandbox Code Playgroud)
但我发现这是不可能的.GCC给出以下错误:
错误:'char'之前的预期unqualified-id
你是否真的无法在for循环中初始化不同类型的变量?
我有这样的代码:
template<class ListItem>
static void printList(QList<ListItem>* list)
{
for (auto i = list->size() - 1, j = -1; i >= 0; --i) {
std::cout << i << ", " << j << ": " << list->at(i) << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
当我用g ++ 6.2.1编译它时,我得到以下编译器输出:
test.cpp: In function ‘void printList(QList<T>*)’:
test.cpp:10:7: error: inconsistent deduction for ‘auto’: ‘auto’ and then ‘int’
for (auto i = list->size() - 1, j = -1; i >= 0; --i) {
^~~~
Run Code Online (Sandbox Code Playgroud)
我理解这一点,如果变量有不同的类型auto i = 0.0, j …
为什么这个C++代码不能在VS2010下编译:
for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {}
Run Code Online (Sandbox Code Playgroud)
虽然这个做了:
short b = 0;
for ( int a = 0; a < 10; ++a, ++b ) {}
Run Code Online (Sandbox Code Playgroud)
是否禁止在for-loop初始化程序中声明两个不同类型的变量?如果是这样,你怎么解决它?
一个明显的(天真的?)方法是:
std::set<int> s;
for (int i = 0; i < SIZE; ++i) {
s.insert(i);
}
Run Code Online (Sandbox Code Playgroud)
这是合理的可读性,但从我的理解,不是最优的,因为它涉及重复搜索插入位置并且没有利用输入序列已经排序的事实.
是否有更优雅/高效(或事实上)的方式来初始化std::set一系列数字?
或者,更一般地说,如何有效地将有序的条目列表插入到集合中?
浏览文档,我刚刚注意到接受迭代器的构造函数来指示插入的位置:
iterator insert ( iterator position, const value_type& x );
Run Code Online (Sandbox Code Playgroud)
这意味着这将更有效:
std::set<int> s;
std::set<int>::iterator it = s.begin();
for (int i = 0; i < SIZE; ++i) {
it = s.insert(it, i);
}
Run Code Online (Sandbox Code Playgroud)
这看起来合理,但我仍然愿意接受更多建议.
以下是无效代码:
int i = 0, double j = 2.0;
Run Code Online (Sandbox Code Playgroud)
标准草案说明了原因:
[N4140/7.1.6]
2作为一般规则,最多一个类型说明符被允许在完全DECL说明符-SEQ声明的或在一个 类型说明符序列或尾随类型说明符-SEQ.此规则的唯一例外情况如下:-
const可以与除自身之外的任何类型说明符组合使用.-
volatile可以与除自身之外的任何类型说明符组合使用.-
signed或unsigned可以结合char,long,short,或int.-
short或者long可以结合使用int.-
long可以结合使用double.-
long可以结合使用long.
是的,它可以防止愚蠢的事情int int,但我发现上面发布的无效代码没有任何问题.引用[N4140/7],简单声明包含一个decl-specifier-seq opt init-declarator-list opt ;
[N4140/8]然后表明init-declarator-list包含一个init-declarator-list,init-declarator,
和初始化声明符 …
您可以在for循环中定义2个相同类型的变量:
int main() {
for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
但是定义不同类型的变量是违法的:
int main() {
for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?(我不需要i在循环内使用,只是j.)
如果你完全被黑客攻击并且模糊不清,那对我来说没问题.
在这个人为的例子中,我知道你可以使用double这两个变量.我正在寻找一般答案.
请不要建议移动body之外的任何变量,可能对我不可用,因为一个迭代器必须在循环之后消失并且for语句将被包含在我的foreach宏中:
#define foreach(var, iter, instr) { \
typeof(iter) var##IT = …Run Code Online (Sandbox Code Playgroud) 我想写一个for循环,如下所示; 在初始化部分,我想声明不同类型的变量:
for (int loop=0, long result = 1; loop <= 10 ; loop++, result *= 2 )
{
cout << "2^"<<loop<<"=" << result<<endl;
}
Run Code Online (Sandbox Code Playgroud)
但它给出错误,意味着它是不允许的.任何解决方案?
我很困惑," 'iter'未在此范围内声明 "错误.
#include <vector>
using std::vector;
int main()
{
vector<int> vec{1,2,3,4,5,6};
for(std::size_t i,vector<int>::iterator iter=vec.begin();iter!=vec.end();++i,++iter)
{
//do something
}
}
Run Code Online (Sandbox Code Playgroud)