Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何打印成对向量数组的元素?_C++_Stl - Fatal编程技术网

C++ 如何打印成对向量数组的元素?

C++ 如何打印成对向量数组的元素?,c++,stl,C++,Stl,我有一个向量对数组 vector <pair<int,int> > a[4] 向量a[4] 。我已使用push_back向其中添加了元素。但我不知道如何打印元素。如果我使用iTertor并像[I]一样打印它。第一个或[I]。第二个会出错。是否有其他方法。请提前感谢 vector <pair<int,int> > a[4]; for(int i = 0;i < e;++i) { int x,y; cin >> x

我有一个向量对数组

vector <pair<int,int> > a[4]
向量a[4]
。我已使用push_back向其中添加了元素。但我不知道如何打印元素。如果我使用iTertor并像[I]一样打印它。第一个或[I]。第二个会出错。是否有其他方法。请提前感谢

vector <pair<int,int> > a[4];
for(int i = 0;i < e;++i)
{
    int x,y;
    cin >> x >> y >> w;
    a[x].push_back({w, y});
    a[y].push_back({w, x});
}
向量a[4];
对于(int i=0;i>x>>y>>w;
a[x]。推回({w,y});
a[y]。推回({w,x});
}
这就是我推送元素的方式。但是如何打印它们

for(i=a[i].begin();i!=a[i].end();i++)
{
    cout<<a[i].second<<" ";
}
error: no match for 'operator[]' (operand types are 'std::vector<std::pair<int, int> >*' and 'std::vector<std::pair<int, int> >::iterator {aka __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >}')
  for(i=g[i].begin();i!=g[i].end();i++)
for(i=a[i].begin();i!=a[i].end();i++)
{

您没有提供任何代码,以便我们能够知道您的机器出了什么问题。 但这里有一个关于如何访问向量对的工作示例:

#include <utility>
#include <iostream>
#include <vector>
#include <string>

typedef std::pair<int, std::string> pairIntString;
int main()
{
   std::vector<pairIntString> pairVec;

   pairVec.push_back(std::make_pair(1, "One"));
   pairVec.push_back(std::make_pair(2, "Two"));
   pairVec.push_back(std::make_pair(3, "Three"));

   for (std::vector<pairIntString>::const_iterator iter = pairVec.begin();
        iter != pairVec.end();
        ++iter)
   {
      std::cout << "First: "    << iter->first
                << ", Second: " << iter->second <<std::endl;
   }
   return 0;
}
编辑#1:

现在您提供了代码,但实际上您使用的是一组向量对:
向量a[4];
。此外,您还将
begin()
方法中的
迭代器
放入
[]操作符中。
。看起来您混合了很多东西,例如这里的
i=a[i]。begin()
(一个
i
是迭代器,另一个是索引),但不了解它们的真正用途。请看我的示例,了解数组和向量以及如何正确访问它们。同时了解索引访问和基于迭代器的访问的区别

编辑#2:

此循环:

for(i=a[i].begin();i!=a[i].end();i++)
{
    cout<<a[i].second<<" ";
}
for(i=a[i].begin();i!=a[i].end();i++)
{
cout
vec向量[5];
向量::迭代器;
对于(inti=0;i
vectorv;
intn,in1,in2;
cin>>n;
对于(int i=0;i>in1>>in2;
v、 向后推(形成一对(in1,in2));
}
对于(int i=0;icout我在一个问题中使用了这个概念,你可以检查一下代码,它们与你想要的一样

#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define loop(i, k, n) for(int i = k; k < n ? i < n : i >= n; k < n ? i+=1 : i-=1)
#define pll pair<ll, ll>
#define pb push_back
#define po pop_back
#define mp make_pair
#define F first
#define S second
#define endl "\n"

int main()
{
    ll n, k;
    cin >> n >> k;
    ll arr[n];
    ll ansArr[n] = {0};
    vector <pair<ll, ll>> v[n];
    loop(i, 0, n) cin >> arr[i];
    loop(i, 0, n) {
        if(arr[i] < 0) {
            for(int j = i+1; j <= i+k && j < n; j++) {
                if(arr[j] > 0) v[j].pb(mp(arr[i], i)); // Taking input in pairs
            }
            for(int j = i-1; j >= i-k && j >= 0; j--) {
                if(arr[j] > 0) v[j].pb(mp(arr[i], i)); // Taking input in pairs
            }
        }
    }
    // Printing the result
    loop(i, 0, n) {
        for(auto it : v[i]) cout << it.F << " ";
        cout << endl;
        for(auto it : v[i]) cout << it.S << " ";
        cout << endl;
    }

    return 0;
}
#包括
使用名称空间std;
#定义ll long long
#定义无符号长
#为(int i=k;k=n;k>n>>k;
ll arr[n];
ll ansArr[n]={0};
向量v[n];
环(i,0,n)cin>>arr[i];
循环(i,0,n){
if(arr[i]<0){
for(int j=i+1;j 0)v[j].pb(mp(arr[i],i));//成对输入
}
对于(int j=i-1;j>=i-k&&j>=0;j--){
if(arr[j]>0)v[j].pb(mp(arr[i],i));//成对输入
}
}
}
//打印结果
循环(i,0,n){

用于(自动it:v[i])欢迎来到Stack Overflow。请花点时间阅读并参考“您可以在此处询问什么和如何提问”中的材料。为什么这是我不应该在此处提问的主题?您确定要有一个
向量数组吗?请用一个我的帖子。如何打印它,我需要它。我需要用它来表示邻接表。你能告诉我如何打印EntEnSya吗?我在C++中使用STL是新的。这就是为什么这些混乱。谢谢你已经给出的代码。我不能投票赞成(我只有一个名声)。。非常感谢您提供了快速正确的答案。虽然此代码可能提供了问题的解决方案,但最好添加有关其工作原因/方式的上下文。这可以帮助未来用户学习,并将这些知识应用到他们自己的代码中。在解释代码时,用户也可能会以投票的形式给予积极反馈。
/* loop through the fixed size array */
for(size_t idx = 0; idx < 4; ++i)
{
   cout << "Array element idx: " << idx << endl;
   /* get the iterator of the specific array element */
   for (vector <pair<int,int> >::const_iterator iter = a[idx].begin();
        iter != a[idx].end();
        ++iter)
   {
      cout << "First: "    << iter->first
           << ", Second: " << iter->second << endl;
   }
}
vector <pair<int,int>> vec[5];
vector <pair<int,int>> :: iterator it;

for(int i=0;i<5;i++){
    for(it=vec[i].begin(); it!=vec[i].end();it++) cout << it->first << " " << it->second << " -> ";
    cout << "\n";
}
vector<pair<int,int>> v;
int n , in1 , in2;
cin >> n;

for(int i = 0 ; i < n ; i++)
{
    cin >> in1 >> in2;
    v.push_back(make_pair(in1,in2));
}

for(int i = 0 ; i < n ; i++)
    cout << v[i].first << " " << v[i].second << endl;

return 0;
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define loop(i, k, n) for(int i = k; k < n ? i < n : i >= n; k < n ? i+=1 : i-=1)
#define pll pair<ll, ll>
#define pb push_back
#define po pop_back
#define mp make_pair
#define F first
#define S second
#define endl "\n"

int main()
{
    ll n, k;
    cin >> n >> k;
    ll arr[n];
    ll ansArr[n] = {0};
    vector <pair<ll, ll>> v[n];
    loop(i, 0, n) cin >> arr[i];
    loop(i, 0, n) {
        if(arr[i] < 0) {
            for(int j = i+1; j <= i+k && j < n; j++) {
                if(arr[j] > 0) v[j].pb(mp(arr[i], i)); // Taking input in pairs
            }
            for(int j = i-1; j >= i-k && j >= 0; j--) {
                if(arr[j] > 0) v[j].pb(mp(arr[i], i)); // Taking input in pairs
            }
        }
    }
    // Printing the result
    loop(i, 0, n) {
        for(auto it : v[i]) cout << it.F << " ";
        cout << endl;
        for(auto it : v[i]) cout << it.S << " ";
        cout << endl;
    }

    return 0;
}