打印列表的元素数量如何。

huangapple go评论52阅读模式
英文:

How to print the number of elements of a list

问题

我有以下代码:

#include <iostream>
#include <list>
using namespace std;

class YTchannel {
public:
    string name;
    string owner;
    int subs;
    list<string> video_title;
};

int main() {

    YTchannel ytc;
    ytc.video_title = {"A", "B", "C"};
    for (string videotitle : ytc.video_title) {
        for (int i = 1; i <= videotitle.size(); i++) {
            cout << i << videotitle << endl;
            break;
        }
    }
}

我想显示视频标题的列表以及它们的相应编号:
1A
2B
3C

但如果我运行这段代码,我会得到:
1A
1B
1C

英文:

I have the code below:

#include &lt;iostream&gt;
#include &lt;list&gt;
using namespace std;

class YTchannel{
public: 
    string name;
    string owner;
    int subs;
    list&lt;string&gt; video_title;
};

int main(){

    YTchannel ytc; 
    ytc.video_title={&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
    for(string videotitle: ytc.video_title){
        for(int i=1;i&lt;=videotitle.size();i++){
            cout&lt;&lt;i&lt;&lt;videotitle&lt;&lt;endl;
            break;
        }
    }

I want to display the list of video titles with their respective number:
1A
2B
3C

But if I run the code, i'll obtain:
1A
1B
1C

答案1

得分: 4

第一个循环:

for (string videotitle : ytc.video_title)

遍历列表中的所有字符串。

第二个循环:

for (int i = 1; i <= videotitle.size(); i++)

遍历当前字符串中的所有字符。在该循环中,您在每次迭代中打印整个字符串。

简单的解决方案是在第一个循环中保留一个单独的计数器,您在其中递增:

unsigned counter = 1;
for (auto const& title : yts.video_titles)
{
    std::cout << counter++ << ' ' << title << '\n';
}
英文:

The frist loop:

for(string videotitle: ytc.video_title)

iterates over all the strings in the list.

The second loop:

for(int i=1;i&lt;=videotitle.size();i++)

iterates over all characters in the current string. And in that loop you print the whole string each iteration.

The simple solution is to keep a separate counter that you increase in the one single first loop:

unsigned counter = 1;
for (auto const&amp; title : yts.video_titles)
{
    std::cout &lt;&lt; counter++ &lt;&lt; &#39; &#39; &lt;&lt; title &lt;&lt; &#39;\n&#39;;
}

答案2

得分: 1

你的循环中有一个'break'语句,所以你永远不会增加计数器。

此外,在C++20中,你可以通过在基于范围的循环中使用init语句来缩小作用域。

#include <iostream>
#include <list>
using namespace std;

class YTchannel{
public: 
    string name;
    string owner;
    int subs;
    list<string> video_title;
};

int main(){

    YTchannel ytc; 
    ytc.video_title={"A", "B", "C"};
    int counter = 0; 
    for(string videotitle : ytc.video_title){
        cout<<++counter<<videotitle<<endl;
    }

    // C++20
   //YTchannel ytc; 
   //ytc.video_title={"A", "B", "C"};
   //for(int counter = 0; string videotitle : ytc.video_title){
   //  cout<<++counter<<videotitle<<endl;
   //}
}
英文:

You have a 'break' in your loop so you never increment the counter.

Additionally, in C++20 you can narrow the scope, by using the init statement in range-based loop.

#include &lt;iostream&gt;
#include &lt;list&gt;
using namespace std;

class YTchannel{
public: 
    string name;
    string owner;
    int subs;
    list&lt;string&gt; video_title;
};

int main(){

    YTchannel ytc; 
    ytc.video_title={&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
    int counter = 0; 
    for(string videotitle : ytc.video_title){
        cout&lt;&lt;++counter&lt;&lt;videotitle&lt;&lt;endl;
    }

    // C++20
   //YTchannel ytc; 
   //ytc.video_title={&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
   //for(int counter = 0; string videotitle : ytc.video_title){
   //   cout&lt;&lt;++counter&lt;&lt;videotitle&lt;&lt;endl;
   //}
}

答案3

得分: 0

以下是代码部分的中文翻译:

你正在打印字符串("A""B""C")的长度,而不是列表的长度。你想要的是:
```cxx
    ytc.video_title={"A", "B", "C"};
    auto it = ytc.begin()
    for(size_t i =0; i < ytc.size(); ++i){
        cout<<i<<*it++<<endl;
    }

然而,你可能真正想要的是:

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

// 包含 std::string 的头文件
#include <string>

// 不要使用 "using namespace std"

// 结构体默认情况下都是公共的,所以在这里它们是一个更好的选择而不是类
struct YTchannel{
    std::string name;
    std::string owner;
    int subs;
    // 除非你知道为什么要使用列表,否则使用向量更好
    std::vector<string> video_title;
};

int main(){
    YTchannel ytc; 
    ytc.video_title={"A", "B", "C"};

    for(auto it = ytc.begin(); it != ytc.end(); ++it){
        std::cout << std::distance(ytc.begin(), it) << videotitle << std::endl;
}
英文:

You're printing the string ("A", "B", or "C")'s length - not the list's. You want:

    ytc.video_title={&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
    auto it = ytc.begin()
    for(size_t i =0; i &lt; ytc.size(); ++i){
        cout&lt;&lt;i&lt;&lt;*it++&lt;&lt;endl;
    }

However what you probably really want is:

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

// Include the headers for std::string
#include &lt;string&gt;

// Don&#39;t use &#39;using namespace std&#39;

// Structs are all public by default, so they&#39;re a better choice than a class here
struct YTchannel{
    std::string name;
    std::string owner;
    int subs;
    // Vector is a better choice than list unless you know why it isn&#39;t
    std::vector&lt;string&gt; video_title;
};

int main(){
    YTchannel ytc; 
    ytc.video_title={&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};

    for(auto it = ytc.begin(); it != ytc.end(); ++it){
        std::cout &lt;&lt; std::distance(ytc.begin(), it) &lt;&lt; videotitle &lt;&lt; std::endl;
}

huangapple
  • 本文由 发表于 2023年2月6日 17:15:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定