第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何打印出向量的內(nèi)容?

如何打印出向量的內(nèi)容?

C++
慕桂英546537 2019-07-05 10:04:15
如何打印出向量的內(nèi)容?我想在C+中打印一個(gè)向量的內(nèi)容,下面是我的如下內(nèi)容:#include <iostream>#include <fstream>#include <string>#include <cmath>#include <vector> #include <sstream>#include <cstdio>using namespace std;int main(){     ifstream file("maze.txt");     if (file) {         vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>()));         vector<char> path;         int x = 17;         char entrance = vec.at(16);         char firstsquare = vec.at(x);         if (entrance == 'S') {              path.push_back(entrance);          }         for (x = 17; isalpha(firstsquare); x++) {             path.push_back(firstsquare);         }         for (int i = 0; i < path.size(); i++) {             cout << path[i] << " ";         }         cout << endl;         return 0;     }}如何將向量的內(nèi)容打印到屏幕上?
查看完整描述

3 回答

?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

要做到這一點(diǎn),一個(gè)更簡(jiǎn)單的方法是使用標(biāo)準(zhǔn)復(fù)制算法:

#include <iostream>#include <algorithm> // for copy#include <iterator> // for ostream_iterator#include <vector>int main() {
    /* Set up vector to hold chars a-z */
    std::vector<char> path;
    for (int ch = 'a'; ch <= 'z'; ++ch)
        path.push_back(ch);

    /* Print path vector to console */
    std::copy(path.begin(), path.end(), std::ostream_iterator<char>(std::cout, " "));

    return 0;}

ostream_iterator被稱為迭代器適配器..對(duì)類型進(jìn)行模板化,以便打印到流(在本例中,char). cout(也稱為控制臺(tái)輸出)是我們要寫入的流,以及空格字符(" ")是我們希望在存儲(chǔ)在向量中的每個(gè)元素之間打印的內(nèi)容。

這個(gè)標(biāo)準(zhǔn)算法是強(qiáng)大的,許多其他算法也是如此。標(biāo)準(zhǔn)庫(kù)給你的力量和靈活性使它如此偉大。想象一下:您可以用代碼行。您不必使用分隔符來(lái)處理特殊情況。你不需要擔(dān)心循環(huán)。標(biāo)準(zhǔn)圖書館為你做這一切。


查看完整回答
反對(duì) 回復(fù) 2019-07-05
?
波斯汪

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊

在C+11中,您現(xiàn)在可以使用基于范圍的循環(huán):

for (auto const& c : path)
    std::cout << c << ' ';


查看完整回答
反對(duì) 回復(fù) 2019-07-05
  • 3 回答
  • 0 關(guān)注
  • 898 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)