【问题标题】:How to print elements of array of vectors of pairs?如何打印成对向量数组的元素?
【发布时间】:2017-11-19 04:10:36
【问题描述】:

我有一个成对向量数组

vector <pair<int,int> > a[4]

。我使用 push_back 向它添加了元素。但我不知道如何打印元素。如果我使用迭代器并像 a[i].first 或 a[i].second 那样打印它会抛出错误。任何其他方式。在此先感谢。

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});
}

这是我推送元素的方式。但是如何打印它们。

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++)

【问题讨论】:

标签: c++ stl


【解决方案1】:

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

#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;
}

输出,see here:

First: 1, Second: One
First: 2, Second: Two
First: 3, Second: Three

编辑#1:

现在您提供了代码,但实际上您使用的是成对向量数组:vector &lt;pair&lt;int,int&gt; &gt; a[4];。此外,您将begin() 方法中的iterator 放入[] operator。似乎你混合了很多东西,例如这里i=a[i].begin()(一个i 是迭代器,另一个是索引)并且不明白它们的真正用途。请查看我的示例并阅读有关数组和向量以及如何正确访问它们的信息。另请阅读索引访问和基于迭代器访问的区别。

编辑#2:

这个循环:

for(i=a[i].begin();i!=a[i].end();i++)
{
    cout<<a[i].second<<" ";
}

应该是:

/* 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;
   }
}

当你有一个成对的向量数组时,你有两个循环遍历数组和向量。因为数组的大小固定为4,所以我将其用作最大值。

【讨论】:

  • 我已经在我的帖子中进行了更改。如何为这种情况打印
  • 不,我想要它。我需要用它来表示邻接表。你能告诉我如何打印元素
  • 是的,我是在 c++ 中使用 stl 的新手。这就是这些混淆的原因。
  • 感谢您提供的代码。我无法投票(我只有一个声誉)。
  • 非常感谢您快速而正确的回答。
【解决方案2】:
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";
}

【讨论】:

    【解决方案3】:
    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;
    

    【讨论】:

    • 虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并将这些知识应用到他们自己的代码中。在解释代码时,您也可能会以赞成票的形式从用户那里获得积极的反馈。
    【解决方案4】:

    我在一个问题中使用了这个概念,您可以查看执行相同操作的代码

    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2019-07-22
      • 2021-10-30
      • 2018-04-09
      • 2020-04-23
      相关资源
      最近更新 更多