void Print(vector <string>)函数无法打印

时间:2018-09-10 02:28:27

标签: c++ string vector stdvector cout

我做了一个使用插入器打印矢量的功能。该函数是程序的一部分,该程序旨在将字符串向量复制到另一个字符串向量。原始函数是使用.at()成员函数的简单for循环,并且有效。因此,我不确定这是怎么回事。 代码:

extension UIImage {
    func withInsets(_ insets: UIEdgeInsets) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(
            CGSize(width: size.width + insets.left + insets.right,
                   height: size.height + insets.top + insets.bottom),
            false,
            self.scale)

        let origin = CGPoint(x: insets.left, y: insets.top)
        self.draw(at: origin)
        let imageWithInsets = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return imageWithInsets
    }
}

输出:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavBar(with: backImage)
    }

    func setupNavBar(with backIcon: UIImage) {
        navBarLeftImageInsects = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()

        if let backImage = backIcon.withInsets(navBarLeftImageInsects) {
            navigationController?.navigationBar.backIndicatorImage = backImage
            navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
            navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试此简单更改以初始化newVect。如评论所述,您还可以resize()newVect

#include <string>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void print(vector<string> &v);

int main() {
    ifstream inFS;
    inFS.open("sentence.txt");

    vector<string> wordList; 
    string s;
    while (inFS >> s) {
        wordList.push_back(s);
    }

    vector<string> newVect(wordList);
    print(wordList);
    print(newVect);
}


void print(vector<string> &v) {
    for (vector<string>::iterator i = v.begin(); i != v.end(); i++) {
        cout << *i << " ";
    }
    cout << endl;
}