排序后跟踪矢量对索引

时间:2015-10-12 19:09:22

标签: c++ stl

之前已经提出了类似的问题,但我在应用这个概念时遇到了麻烦。我想跟踪对向量中对的索引,然后排序,std :: cout输入顺序。

e.g。

cin >> 5 6 1 5 1 2 1 2 3 5
cout << 1 2 1 2 1 5 3 5 5 6
cout >> 3 4 2 5 1  //and this is where I am having trouble.

我正在考虑转换为三元组,其中一个元素是索引,但我无法满足我的需求。

任何帮助将不胜感激。谢谢。

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>

using namespace std;

bool compare(const pair<int,int>&A, const pair<int,int>&B);

int main()
{
    vector<pair<int,int>> v;
    pair<int,int> tok;
    while(cin >> tok.first>>tok.second){v.push_back(tok);}

    sort(v.begin(),v.end(), compare);
    for(int i = (signed int)v.size()-1; i >= 0 ; i--)
    {
        cout << v.at(i).first << " ";
        cout << v.at(i).second << " ";
    }
    cout << endl;
    return 0;
}

bool compare(const pair<int,int>&A, const pair<int,int>&B)
{
    return A.first > B.first;
}

1 个答案:

答案 0 :(得分:0)

这是一种方法。很久以来我为他们做了某人的功课; - )

package sandbox;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import java.io.PrintStream;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.XfaForm;


public class FillXFA {

    public static final String SRC = "C:/Workspace/PDF/incomplete.pdf";
    public static final String XML = "C:/Workspace/PDF/data.xml";
    public static final String DEST = "C:/Workspace/PDF/completed.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillXFA().manipulatePdf(SRC, DEST);
    }

    public void readXfa(String src, String dest)
            throws IOException, ParserConfigurationException, SAXException,
                TransformerFactoryConfigurationError, TransformerException {
            FileOutputStream os = new FileOutputStream(dest);
            PdfReader reader = new PdfReader(src);
            XfaForm xfa = new XfaForm(reader);
            Document doc = xfa.getDomDocument();
            Transformer tf = TransformerFactory.newInstance().newTransformer();
            tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            tf.transform(new DOMSource(doc), new StreamResult(os));
            reader.close();
        }

    public void manipulatePdf(String src, String dest)
        throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader,
                new FileOutputStream(dest));
        AcroFields form = stamper.getAcroFields();
        XfaForm xfa = form.getXfa();
        xfa.fillXfaForm(new FileInputStream(XML));
        stamper.close();
        reader.close();
    }
}