Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
作为Cout(C+;+;)的一部分跟踪/显示数组索引 < >我有一个命令行C++程序,让你输入一个人的基本信息(ID号、姓名、年龄等),我想以以下方式输出到控制台: ------------------------------------------------------------------- Index ID # First Name Last Name Age ------------------------------------------------------------------- 0 1234 John Smith 25_C++ - Fatal编程技术网

作为Cout(C+;+;)的一部分跟踪/显示数组索引 < >我有一个命令行C++程序,让你输入一个人的基本信息(ID号、姓名、年龄等),我想以以下方式输出到控制台: ------------------------------------------------------------------- Index ID # First Name Last Name Age ------------------------------------------------------------------- 0 1234 John Smith 25

作为Cout(C+;+;)的一部分跟踪/显示数组索引 < >我有一个命令行C++程序,让你输入一个人的基本信息(ID号、姓名、年龄等),我想以以下方式输出到控制台: ------------------------------------------------------------------- Index ID # First Name Last Name Age ------------------------------------------------------------------- 0 1234 John Smith 25,c++,C++,person对象存储在person数组中,我已重载ostream(编辑: 现在我知道你想要什么了。你想要在向量中找到一个元素 std::vector<person>::iterator p = std::find(Persons.begin(), Persons.end(), element); if( p != Persons.end() ) { std::cout << "index of element is: " << p-

person对象存储在person数组中,我已重载ostream(编辑:

现在我知道你想要什么了。你想要在向量中找到一个元素

std::vector<person>::iterator p = 
          std::find(Persons.begin(), Persons.end(), element);

if( p != Persons.end() )
{
  std::cout << "index of element is: " << p-Persons.begin();
}
std::vector::iterator p=
std::find(Persons.begin()、Persons.end()、元素);
if(p!=Persons.end())
{
std::cout这不起作用吗?(ID字段的格式未指定)

向量v; 对于(int i=0;i不能你需要使用“查找”在vector中查找Person对象确切索引的算法

您可以使用包装器类保存索引并根据
operatorRight中的格式进行打印,这就是我现在使用的方法。我使用了setw、left、right、setfill等。我的问题是尝试打印数组元素的索引值。通常在Java或C,我会做数组。索引元素(元素),它会给我元素的索引。C++中有等价的吗?我需要的更简单(见我自己的答案),但非常感谢您在向量中使用find函数。我也在想怎么做。我也能在数组中使用find函数吗?@John:是的,这就是STL容器/算法分离的威力。您只需要传递表示内容开头和结尾的迭代器,指针可以用作迭代器。因此,查找(数组、数组+长度、谓词)或以不同的顺序查找这些参数。我真的不清楚您要做什么。如何将索引传递给cout的提取运算符(运算符告诉我们重载的find是否适用于数组或仅适用于向量?它可以搜索数组和容器。您可以在这里找到示例:出于某种原因,在我重新阅读您的代码之前,我一直没有找到最明显的答案,Eli。它太简单了……我所要做的就是转到For循环,在那里我打印出每一行并让它打印出来。)由于某种原因,当我试图将索引与每个Person对象相关联时,我从未想到过这一点。非常感谢,我将尽量不再犯这个错误。
for(size_t i = 0; i < Persons.size(); ++i)
{
  cout << i << '\t' << Persons[i] << endl;
}
vector<Person> v;

for (int i = 0; i < v.size(); ++i)
  cout << i + 1 << v[i] << endl;
// wrapper to hold index
template<typename T>
struct Ti
{
    Ti( size_t index, const T& t ) : index(index), val(t) {}
    size_t index;
    const T& val;
};

// you class
struct X
{
    friend ostream& operator<<( ostream& out, Ti<X>& t );
protected:
    int some_data;
};

// operator<< for X
ostream& operator<<( ostream& out, Ti<X>& t )
{
    out << "test " << t.index << "  " << t.val.some_data;
    return out;     
}

int main()
{
    vector<X> xxx;
    for ( size_t i =0; i < xxx.size(); ++i)
        cout << Ti<X>(i+1, xxx[i]) << endl;
}