使用c ++中的向量对进行排序(sorting using vector pair in c++)
我正在使用向量对来排序我使用循环插入第一个和第二个元素的位置
vect.push_back(make_pair(count[i],arr[i])); sort(vect.begin(),vect.end()); cout<<vect[i].second<<" ";
如果我的计数是相等的,那么我想按照它们在矢量中插入的顺序打印第二个元素,
但它没有发生。
有人可以告诉我为什么吗?
I am using vector pair to sort where I am inserting first and second element using a loop
vect.push_back(make_pair(count[i],arr[i])); sort(vect.begin(),vect.end()); cout<<vect[i].second<<" ";
if my counts are equal then I want to print the second element in the order they were inserted in vector,
but it is not happening.
Can someome tell me why?
原文:https://stackoverflow.com/questions/49490137
更新时间:2021-12-07 13:12
最满意答案
您可以使用数组上的index(of:)方法来获取索引
if let index = hashtagArray.index(of: "SOMESTRING") { hashtagArray.remove(at: index) }
You can use the index(of: ) method on the array to get the index
if let index = hashtagArray.index(of: "SOMESTRING") { hashtagArray.remove(at: index) }
相关问答
更多-
你可以使用filter()来过滤你的数组 var myArrayOfStrings = ["Hello","Playground","World"] myArrayOfStrings = myArrayOfStrings.filter{$0 != "Hello"} print(myArrayOfStrings) // "[Playground, World]" You can use filter() to filter your array as follow var strings = ["H ...
-
var array = ["ExampleString1", "ExampleString2", "ExampleString3"] let indexPath = 0 // Your indexPath let newArray = array.enumerate().filter { $0.index > indexPath }.map { $0.element } 像这样的东西。 并使用Paulw11的建议:(如果他发布他的答案,我会删除我的) var newArray: [String] = [ ...
-
您可以使用数组上的index(of:)方法来获取索引 if let index = hashtagArray.index(of: "SOMESTRING") { hashtagArray.remove(at: index) } You can use the index(of: ) method on the array to get the index if let index = hashtagArray.index(of: "SOMESTRING") { hashtagArray.r ...
-
而不是黑客攻击的东西,只需要问tableView告诉你给定单元格的indexPath : // use indexPath(for:) on tableView let indexPath = tableView.indexPath(for: walletTableViewCell) // then you can simply use it let crypto = cryptosArray[indexPath.row] UITableView.indexPath(for:) 文档说: 返回表示给定表 ...
-
首先,使用sender可能会引起混淆; 通常发件人是导致事件的人,而不是传送任意数据。 我会用这个改进你的代码: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { performSegue(withIdentifier: "CommentSegue", sender: tableView) // ... } override func prepare(for segue: UISt ...
-
所以答案就是删除删除行的行。 所以代码在这里删除: func tableView(profileTableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if (editingStyle == UITableViewCellEditingStyle.Delete) { ...
-
let userImages = userposts[indexPath.row] 在代码中的这一点上, indexPath.row > 0。 数组是从0开始的,所以第一个单元格( indexPath.row == 1 )获取数组中的第二个项目( user posts[1] ),这是你想要的第二个图像。 我可以想到几个简单的变化: 更改您正在访问的索引,例如: let userImages = userposts[indexPath.row - 1] 在numberOfItemsInSection:的use ...
-
你应该总是知道你的字典键。 如果不这样做,那么您构建的数据就是错误的 - 未知数据应始终位于字典的值中,而不是键。 请考虑使用带有两个键的字典:“username”和“flag”。 示例代码: var users = [[String:String]]() users.append(["username" : "Aaron", "flag" : "yes"]) users.append(["username" : "AspiringDeveloper", "flag" : "yes"]) let use ...
-
在AppDelegate applicationDidFinishLaunching将一个空数组注册为键“NewArray”的默认值。 let defaultValues = ["NewArray": [String]()] UserDefaults.standard.register(defaults: defaultValues) 在第二个VC中,定义newArray (变量名称应该以小写字母开头)也作为空String数组 var newArray = [String]() 在viewDidLoad ...
-
从包含swift中特定字符串的数组中删除元素(Remove elements from an array that contains particular string in swift)[2022-05-23]
您可以使用过滤方法: array_var = array_var.filter { $0.rangeOfString("convert?fit=crop") == nil } You can use filter method: array_var = array_var.filter { $0.rangeOfString("convert?fit=crop") == nil }