在 C++ 中增加 List 的所有元素
Increase all elements of a List in C++
#include <iostream>
#include <vector>
#include <iterator>
#include <iostream>
#include <cmath>
#include <string>
#include <utility>
#include <cstring>
#include <list>
using std::vector;
using std::cout;
using std::list;
using std::endl;
using std::string;
using namespace std;
template <typename T>
void showContents(T& input)
{
typename T::iterator it;
for (it=input.begin(); it != input.end(); it++)
{ cout << *it << " "; }
cout << endl;
}
int main()
{
int B[10] = {0,1,2,3,4,5,6,7,8,9};
cout<< "The first array is: "<< "\n";
int i;
for (i = 0; i<10; i++)
{cout<< B[i]<< " ";}
vector<int> KVec(B,B+10);
cout << "\n \n" << "The first vector is: " << endl;
showContents(KVec);
list<int> BList(B,B+10);
cout << "\n" << "The first list is: " << endl;
showContents(BList);
int BCopy [10];
cout<< "\n" <<"The second array is: "<< endl;
for(int i = 0; i <10; i++)
{
BCopy[i] = B[i];
BCopy[i] += 2;
cout<< BCopy[i]<< " ";
}
vector<int> KVec2(KVec);
cout<< "\n \n" << "The second vector is: "<< endl;
for (int i = 0; i<KVec2.size(); i++){
KVec2[i] += 3;
}
showContents(KVec2);
cout<< "\n" << "The second list is: "<< endl;
std::list<int> BList2 (BList);
for (std::list<int>::iterator b = BList.begin(); b!=BList.end(); ++b)
{
( *b += 5 );
showContents(BList2);
}
这是我的代码。我能够正确地复制所有数组、向量和列表,并相应地增加它们的值。我无法在列表中增加的唯一一个。我的目标是将第二个列表的所有元素递增 5。我一直在使用多个引用来尝试这样做,但我已经尝试了所有方法,但无法让它工作。下面是我最近尝试增加所有值的尝试,但这似乎也不起作用,所以现在我需要帮助。这是此任务中唯一要做的事情,因此我们将不胜感激。谢谢。
由于我的评论解决了您的问题,我正在将其转换为答案。
您使用 BList
中的值复制构造的 BList2
(我正在更改为大括号初始化以避免 Most vexing parse)。但是随后,您将再次迭代 BList
的值。此外,您不需要 *b += 5
两边的括号。最后,您的 showContents
函数可能在循环之外。
std::list<int> BList2 {BList};
for (std::list<int>::iterator b = BList2.begin(); b != BList2.end(); ++b)
{
*b += 5;
}
showContents(BList2);
#include <iostream>
#include <vector>
#include <iterator>
#include <iostream>
#include <cmath>
#include <string>
#include <utility>
#include <cstring>
#include <list>
using std::vector;
using std::cout;
using std::list;
using std::endl;
using std::string;
using namespace std;
template <typename T>
void showContents(T& input)
{
typename T::iterator it;
for (it=input.begin(); it != input.end(); it++)
{ cout << *it << " "; }
cout << endl;
}
int main()
{
int B[10] = {0,1,2,3,4,5,6,7,8,9};
cout<< "The first array is: "<< "\n";
int i;
for (i = 0; i<10; i++)
{cout<< B[i]<< " ";}
vector<int> KVec(B,B+10);
cout << "\n \n" << "The first vector is: " << endl;
showContents(KVec);
list<int> BList(B,B+10);
cout << "\n" << "The first list is: " << endl;
showContents(BList);
int BCopy [10];
cout<< "\n" <<"The second array is: "<< endl;
for(int i = 0; i <10; i++)
{
BCopy[i] = B[i];
BCopy[i] += 2;
cout<< BCopy[i]<< " ";
}
vector<int> KVec2(KVec);
cout<< "\n \n" << "The second vector is: "<< endl;
for (int i = 0; i<KVec2.size(); i++){
KVec2[i] += 3;
}
showContents(KVec2);
cout<< "\n" << "The second list is: "<< endl;
std::list<int> BList2 (BList);
for (std::list<int>::iterator b = BList.begin(); b!=BList.end(); ++b)
{
( *b += 5 );
showContents(BList2);
}
这是我的代码。我能够正确地复制所有数组、向量和列表,并相应地增加它们的值。我无法在列表中增加的唯一一个。我的目标是将第二个列表的所有元素递增 5。我一直在使用多个引用来尝试这样做,但我已经尝试了所有方法,但无法让它工作。下面是我最近尝试增加所有值的尝试,但这似乎也不起作用,所以现在我需要帮助。这是此任务中唯一要做的事情,因此我们将不胜感激。谢谢。
由于我的评论解决了您的问题,我正在将其转换为答案。
您使用 BList
中的值复制构造的 BList2
(我正在更改为大括号初始化以避免 Most vexing parse)。但是随后,您将再次迭代 BList
的值。此外,您不需要 *b += 5
两边的括号。最后,您的 showContents
函数可能在循环之外。
std::list<int> BList2 {BList};
for (std::list<int>::iterator b = BList2.begin(); b != BList2.end(); ++b)
{
*b += 5;
}
showContents(BList2);