英文:
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 <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;
}
}
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<=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& title : yts.video_titles)
{
std::cout << counter++ << ' ' << title << '\n';
}
答案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 <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;
//}
}
答案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={"A", "B", "C"};
auto it = ytc.begin()
for(size_t i =0; i < ytc.size(); ++i){
cout<<i<<*it++<<endl;
}
However what you probably really want is:
#include <iostream>
#include <vector>
#include <algorithm>
// Include the headers for std::string
#include <string>
// Don't use 'using namespace std'
// Structs are all public by default, so they'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't
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论