簡體   English   中英

使用std :: generate_n使用字母字符填充向量

[英]Fill a vector with alphabet chars using std::generate_n

我想用字母填充一個向量。 所以我寫了以下內容:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

char i='a';
char f(){return i++;};

int main()
{

  std::vector<char> lol(24);

  std::generate_n(lol.begin(),lol.size(),f);

  std::copy(lol.begin(),lol.end(),std::ostream_iterator<int>(std::cout,","));

}

但是std :: copy打印出97,98,99...字母的ASCII碼。 我怎樣才能解決這個問題?

您的ostream_iterator是<int>而不是<char> -嘗試將其更改為<char>

將模板專用化更改為char: std::copy(lol.begin(),lol.end(),std::ostream_iterator<char>(std::cout,","));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM