如何从数组C ++获取唯一字符串

编程入门 行业动态 更新时间:2024-10-17 21:15:15
本文介绍了如何从数组C ++获取唯一字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道我的问题对某人可能是愚蠢的,但是我整日用Google搜寻并尝试制定自己的解决方案,但我失败了..请帮助..

i know my question can be stupid for somebody, but i googled all day and try make my own solution but i failed.. Please help..

我需要从简单的字符串数组中打印所有uniqe字符串.

I need print all uniqe string from an simple array of strings.

示例:

输入:嗨"我的"名字"嗨"土豆"文本"名字"嗨"

input: "Hi" "my" "name" "Hi" "potato" "text" "name" "Hi"

输出:我的",土豆",文本"

output: "my" "potato" "text"

我使函数只打印一次所有内容("Hi","my","name","potato","text"),但是我需要忽略数组中2倍或更多次的所有内容.

I make just function to print everything once ("Hi", "my", "name", "potato", "text"), but i need ignore everything what is 2x and more times in array.

我的算法是: 1.按冒泡排序

My algorythm was: 1. sort by bubblesort

  • 使用basic for和if仅打印排序序列中的最后一个字符串
  • .. if(array [i]!= array [i + 1])//做点事情...

    .. if(array[i]!=array[i+1]) //make something...

    推荐答案

    update:我误解了这个问题,现在它可以作为问题的输出 您只需计算每个字符串的出现次数,并仅打印出现的字符串即可. 时间复杂度:O(N ^ 2) 这是代码

    update : i was misunderstanding the question now it works as output in the question simply you can count the occurrences of every string and print only strings which occur ones.. time complexity : O(N^2) here is the code

    #include<iostream> #include<set> #include <string> #include <vector> using namespace std; int main() { int n; // number of strings you want in your array cin >> n; string t; // get t and push back it in the vector vector <string> words; //we use vector to store as we will push back them for(size_t i = 1;i <= n;i++) { cin >> t; words.push_back(t); } for(int i = 0;i < words.size();i++) { int cnt = 0; for(int j = 0;j < words.size() && cnt < 2;j++) { if(words[i] == words[j]) cnt++; } if(cnt == 1) //its unique..print it cout << words[i] <<endl; } }

    更多推荐

    如何从数组C ++获取唯一字符串

    本文发布于:2023-11-29 12:35:44,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1646413.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数组   字符串

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!