简体   繁体   English

有没有更有效的比较字符串的方法?

[英]Is there a more efficient way of comparing strings?

I am creating a program that stores 3 film objects in an array . 我正在创建一个程序,将3个电影对象存储在一个array As part of it there is a method that searches for films that have the same 2 actors supplied by the user. 作为其一部分,有一种方法可以搜索用户提供的具有相同2个演员的电影。 The actual actors in the object are simply stored as variables ( _actor1 and _actor2 ). 对象中的实际参与者仅存储为变量( _actor1_actor2 )。 This is my code: 这是我的代码:

static void sameActors(string actor1, string actor2)
    {
        foreach (Film i in filmLibrary)
        {
            //check if both actors are in the film
            if ((i.getActor1() == actor1 && i.getActor2() == actor2) || (i.getActor1() == actor2 && i.getActor2() == actor1))
            {
                foreach (Film j in filmLibrary)
                {
                    //makes sure that it does not compare to itself
                    if (i.getName() != j.getName())
                    {
                        //checks if films have same actors
                        if ((actor1.Equals(j.getActor1()) && actor2.Equals(j.getActor2())) || (actor2.Equals(j.getActor1()) && actor2.Equals(j.getActor2())))
                        {
                            Console.WriteLine(i.getName() + " and " + j.getName() + " both share the same actors.");
                        }
                    }
                }
            }
        }
        menu();
    }

The code does the job, but to me there is a lot of thinking to do if you are first looking at the code. 代码可以完成这项工作,但是对我来说,如果您是第一次查看代码,则有很多想法要做。 Is there a more efficient way? 有没有更有效的方法?

Also, when this code is executed, it will compare twice, so once it will say " Movie 1 and Movie 2 have the same actors. " and then it will say " Movie 2 and Movie 1 have the same actors. " What's the best way to prevent this? 另外,执行此代码时,它将进行两次比较,因此一旦它说“ 电影1和电影2具有相同的演员。 ”然后会说“ 电影2和电影1具有相同的演员。 ”什么是最好的防止这种情况的方法?

I can't imagine what getActor1() does. 我无法想象getActor1()做什么。 Why would you have a fixed number in the function name? 为什么在函数名称中有固定的数字?

The approach I would take is to store it in a set that supports faster lookups, using either a Dictionary<> or Set<> . 我采用的方法是使用Dictionary<>Set<>将其存储在支持更快查找的Set<> I would store the actors there and then the search should be significant faster. 我会将演员存储在此处,然后搜索速度会更快。 It has the added benefit where it can store any number of actors (since you never know how many actors a movie might have). 它具有额外的优势,可以在其中存储任意数量的演员(因为您永远不知道一部电影可能有多少个演员)。

But, again, it's hard to know why you did it this way so I don't know if that meets your requirements. 但是,再一次,很难知道为什么要这样做,所以我不知道这是否满足您的要求。

I'll elaborate on where I think Jonathan Wood was going. 我将详细说明乔纳森·伍德的去向。 I'm going to include some code that's going to suggest a slightly better model for your data and then also include a solution using LINQ . 我将包含一些代码,这些代码将为您的数据提供一个稍微更好的模型,然后还包含一个使用LINQ的解决方案。 For starters, I think your actors should exist as a collection within your film. 对于初学者来说,我认为您的演员应该作为电影中的收藏存在。 In this way you are not confining your data, and then you allow for a more elegant use of the data. 这样,您就不会限制数据,然后可以更优雅地使用数据。

In this example, I define my Film object, seed 4 films, then create a Dictionary-based index of films to films where actors intersect, excluding itself. 在此示例中,我定义了我的电影对象,给4个电影添加了种子,然后创建了基于词典的电影索引该索引指向演员相交的电影,但不包括演员本身。 Lastly, I report the list using a bit more LINQ to format the movies. 最后,我使用更多的LINQ来报告列表以格式化电影。 In this example there is no restriction on the number of actors per film, and each film can be related to 0 or many other films. 在此示例中,每部电影的演员数量没有限制,每部电影可以与0或许多其他电影相关。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Film> films = new List<Film>() {
                new Film() {
                    Title = "Terminator",
                    Actors = new List<string>() {
                        "Arnold Schwarzenegger",
                        "Michael Biehn",
                        "Linda Hamilton"
                    }
                },
                new Film() {
                    Title = "Aliens",
                    Actors = new List<string>() {
                        "Sigourney Weaver",
                        "Carrie Henn",
                        "Michael Biehn"
                    }
                },
                new Film() {
                    Title = "Avatar",
                    Actors = new List<string>() {
                        "Sam Worthington",
                        "Zoe Saldana",
                        "Sigourney Weaver"
                    }
                },
                new Film() {
                    Title = "Star Wars",
                    Actors = new List<string>() {
                        "Mark Hamill",
                        "Harrison Ford",
                        "Carrie Fisher"
                    }
                }
            };

            var filmIndex = films.ToDictionary(
                f => f.Title, 
                m => films
                    .Where(f => f.Title != m.Title && 
                        f.Actors
                        .Intersect(m.Actors)
                        .Any())
                    .Select(t => t.Title));

            foreach (var film in filmIndex)
            {
                Console.WriteLine(string.Format("Movie: {0} has same actors as {1}.", 
                    film.Key, 
                    film.Value.Any() ? film.Value.Aggregate((i, j) => i + ", " + j) : "no other film"));
            }

            Console.ReadKey();
        }
    }

    class Film
    {
        public string Title { get; set; }
        public IEnumerable<string> Actors { get; set; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM