473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

list class of STL (insertion to the list)

65
list class is said to provide constant-time insertion and removal of elements at any location in the list.
but i cannot insert an element to an arbitrary position in the list using:
Expand|Select|Wrap|Line Numbers
  1. myList.insert(myList.begin()+2500,1000);
i got the following error:
no operator "+" matches these operands
operand types are: std::list<int, std::allocator<int>>::iterator +int myList.insert(myList.begin()+2500,1000);

i think the error message makes sense, because as far as i know list doesnt provide random access.
what i want to ask is "how i can insert to an arbitray position in the list?"
Jul 10 '07 #1
4 1751
mac11
256 100+
i think the error message makes sense, because as far as i know list doesnt provide random access.
what i want to ask is "how i can insert to an arbitray position in the list?"
I'm not sure a list can do what you want, they are made to provide sequential access so you just walk the list to get to the place you want to insert and then call insert and provide your iterator to the place you walked to.

If you need random access maybe a vector or map is better.
Jul 10 '07 #2
The reason it doesn't work is because the you can't do pointer/iterator addition with a linked list. That sort of addition assumes that all elements are in a contiguous memory space such as an array.

In order to insert into an arbitrary position you need an iterator that points to that location. This technically does provide a constant time for inserting at any location, provided that you already have an iterator that points to that location.

You really don't have any choice but to step the iterator through the list to the point you want to insert at, and then use the insert function.
Jul 10 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You really don't have any choice but to step the iterator through the list to the point you want to insert at, and then use the insert function.
That means you (stmfc) need to use a vector and not a list.
Jul 11 '07 #4
That means you (stmfc) need to use a vector and not a list.
true, for the case of inserting in an arbitrary location a vector or other random access data structure is most efficient. But he's using a list, and may have very good reasons to do so.

On a slight note, I have tested out overriding the subscript operator [] to allow pseudo-random access for a linked list. Basically it just used the index provided to step throw the list to the desired node. This concept could be adjusted to return a pointer or iterator to the desired location. It may not be as efficient as a vector, but it shouldn't take that long to step through even a large linked list.
Jul 12 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
4
by: sj | last post by:
I believe the type "list" is implemented as an array of pointers. Thus, random access is an O(1) operation while insertion/deletion is an O(n) operation. That said, I have the following questions:...
4
by: HS-MOON | last post by:
I'm asking you to help me. I'm a beginner of studying c++. I'm trying to make the Singly Linked List(Ordered). Anyway, I've been debugging all day. I can't sort it out!! As you see, I don't...
11
by: velthuijsen | last post by:
I tried taking a list and pass it through std::sort like the following: sort(Unsorted.begin(), Unsorted.end()); I got an error back stating that the list iterator doesn't have a binary...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
6
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create...
4
by: Gaijinco | last post by:
I'm doing a template class to make an ordered list. I created a class Person to make an agenda. When I create a new person, there's no problem but when I try to add it to the list it throws a...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.