C++ int lastIndex[NO_OF_CHARS]={-1}如何;不同于向量<;int>;lastIndex(字符数,-1);?

C++ int lastIndex[NO_OF_CHARS]={-1}如何;不同于向量<;int>;lastIndex(字符数,-1);?,c++,C++,内容如何 int lastIndex[NO_OF_CHARS]={-1}; vector<int> lastIndex(NO_OF_CHARS, -1); 不同于 int lastIndex[NO_OF_CHARS]={-1}; vector<int> lastIndex(NO_OF_CHARS, -1); vector lastIndex(无字符,-1); ? 我如何使用它(摘自@digital_hog的答案评论): #不定义任何字符256 int

内容如何

int lastIndex[NO_OF_CHARS]={-1};  
vector<int> lastIndex(NO_OF_CHARS, -1); 
不同于

int lastIndex[NO_OF_CHARS]={-1};  
vector<int> lastIndex(NO_OF_CHARS, -1); 
vector lastIndex(无字符,-1);
?

我如何使用它(摘自@digital_hog的答案评论):

#不定义任何字符256
int longestuniquesubstr(字符串str){
int n=str.size();
int res=0;
vector lastIndex(无字符,-1);//我想用数组替换它
int i=0;
对于(int j=0;j>t;
而(t--){
字符串s=“”;
cin>>s;
cout
向量lastIndex(无字符,-1);
将使lastIndex看起来像{-1,-1,-1…}(没有字符数)

普通的静态数组也会这样做,区别在于数组的分配方式

int lastIndex[NO_OF_CHARS]={-1};  
声明一个包含
无字符的静态数组,其中第一个元素将从括号内的初始化器列表中复制
{-1}
,所有其他元素将默认初始化(在本例中为0)。因此,最终得到一个数组
[-1,0,…,0]

vector<int> lastIndex(NO_OF_CHARS, -1); 
vector lastIndex(无字符,-1);

使用第二个参数的副本来初始化一个
std::vector
(一个动态数组,如果你愿意的话)。例如,
NO\u OF_CHARS
乘以-1。

谢谢。有没有办法我也可以将-1分配给数组的所有索引。。如果你知道没有字符,你可以用int-lastIndex[NO\u OF_CHARS]={-1,-1,…}来初始化它(聚合中没有字符项)。更优雅的方法是使用for循环并将每个项设置为-1。我尝试过,但结果不同。NO_OF_CHARS=256 for循环可能如下所示:for(int i=0;i#define NO_OF__CHARS 256 int longestuniquessubstr(string str){int n=str.size();int res=0;vector lastIndex(没有字符,-1);//我想用数组int i=0替换它;for(int j=0;j>t;while(t--){string s=“”;cin>>s;coutCheck out-您的数组语法只是初始化我看到的第一个值。。。