在使用数组的for循环 - C ++ [英] Using an array in a for loop - C++
问题描述
我想设置一个测试条件,以确定数组的大小,然后通过在为
循环数组中的每个值移动。
I want to set a test condition to determine the size of an array, and then move through each value of the array in a for
loop.
拿这个数组例如:
std::string expenses[] = {"housing", "utilities", "household expenses", "transportation", "food", "medical", "insurance", "entertainment", "clothing", "misc"};
的括号是空的,里面还有10个元素。你会如何创建这样的为
循环,而无需创建持有的10一个int一个独立的变量?
The brackets are empty, and there are 10 elements in there. How would you create a for
loop for this without creating a separate variable that holds an int of 10?
推荐答案
如果你的意思是通过每个元素运行(相同的范围),那么黑暗是正确的:
If you mean to run through each element (within the same scope), then 'The Dark' is correct:
#include <string>
#include <iostream>
int main()
{
std::string expenses[] = {"housing", "utilities", "household expenses", "transportation", "food", "medical", "insurance", "entertainment", "clothing", "misc"};
std::cout<< "NumEls = " << sizeof(expenses) / sizeof(expenses[0]) << std::endl;
}
产生10个输出,并更换 COUT
与为
循环将使串测试,例如:
produces an output of 10, and replacing the cout
with a for
loop would allow testing of the strings, for example
for (int i=0; i< (sizeof(expenses)/sizeof(expenses[0])); i++)
{
std::cout<< "The " << i << "th string is : " << expenses[i] << std::endl;
}
请注意这将产生0号,1号等......
Note this will produce "0th", "1th", etc...
*买者*
反映了这个问题给出的意见,我们没有答案的事实似乎提不完整的的sizeof(指针)
不会给你有用的信息 - 或者至少,这没有用。因此,如果你想要,而不是使用方法:
Reflecting the comments given in the question, our answers seem incomplete without mention of the fact that the sizeof(POINTER)
won't give you useful information - or at least, not useful for this. As such, if you want instead to use:
myFunction (std::string someArray[])
{
for( all the strings in someArray )
{
std::cout << someArray[i];
}
}
然后你会发现自己无法做到的。
then you'll find yourself unable to do so.
相反,你可以使用:
myFunction (std::string someArray[], int sizeOfArray)
{
for(int i=0; i<sizeOfArray; i++)
{
std::cout<< someArray[i];
}
}
但这样的打击究竟你的问题(而不是存储一个单独的INT)
but this goes exactly against your question (not storing a separate int)
*输入的std ::向量*
有一个简单的解决方案是使用的std ::矢量
A simpler solution is to use a std::vector
使用的载体可以为 myVector.size()
函数调用这样也环路上向量的大小自动根据,在更近的情况下,( C ++ 11)编译器/编译器选项。
The use of a vector allows function calls such as myVector.size()
and also loops based automatically on the size of the vector, in the case of more recent (C++11) compilers/compiler options.
矢量可以愉快地传入和传出的功能,如果你想改变他们,向量引用也是一个简单的方法来做到这一点 - 指的是你的答案:
Vectors can be happily passed into and out of functions, and if you want to change them, references to vectors are also a simple way to do so - referring to your answer:
inputFunction (std::vector<string> &expenses, budget &info)
{
for (int i=0; i< expenses.size(); i++)
{
std::cout<< "Enter your expense for " << expenses[i] << ": ";
// Operation to store input as needed
}
}
在一个侧面说明,好像要在字符串
链接,牺牲为代价的值的名称?如果是这样,也许考虑使用地图。在这种情况下,你可能要考虑的std ::地图&LT;的std ::字符串,浮动方式&gt;
On a side note, it seems like you want to link the string
for the name of the expense to the value of the expense? If so, consider perhaps using a map. In this case, you'd probably want to consider std::map<std::string, float>
.
*使用一个std ::地图*
在使用地图,你可能想的迭代器。一个例子可能是这样的:
In using a map, you'll probably want an iterator. An example might be like:
void input(const std::vector<std::string> &exp, std::map<std::string, float> &map)
{
for (int i=0; i<exp.size(); i++)
{
float tempFloat;
std::cout<< "Please enter the amount for " << exp[i] << ": ";
std::cin >> tempFloat;
map.emplace(exp[i], tempFloat);
}
};
和的main()
,
std::map<std::string, float> myMap;
input(myVec, myMap);
for (std::map<std::string, float>::iterator it=myMap.begin(); it!=myMap.end(); it++)
{
std::cout << "myMap values -> " << it->first << " = " << it->second << std::endl;
}
这将输出每对你有使用起 myMap.begin()的迭代器
并在最后进入到地图的结局。
This will output each pair you have, using an iterator starting at myMap.begin()
and ending at the last entry to your map.
布设(...)
构造一对,然后将其添加到地图中。你应该注意不要使用插入
,这就需要一套不同的参数,而不太可能是你想要的这里。
emplace(...)
constructs a pair, and then adds it to the map. You should take care not to use insert
, which requires a different set of parameters, and is not likely to be what you want here.
的输出由 iterator-&GT引用$ C>第一
和每个地图对第二
值。在这种情况下,那些是字符串
和浮动
存储在地图
。
The outputs are referenced by iterator->first
and iterator->second
, the first
and second
values of each map pair. In this case, those are the string
and float
that are stored in the map
.
这篇关于在使用数组的for循环 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!